I have recently been working a lot with AutoCad for one of our customers. We use .NET (C#) to ”drive” AutoCad and this is done by using the AutoCad .NET API. With the API we can load and modify drawings and automate tasks. It is pretty straight forward for anyone skilled in .NET, BUT it can't do all things because the API is LIMITED in scope. When you get stuck with a problem there aren't many resources on the web that describes what to do. Most examples are either for VB or AutoLISP.

In this very simple example I show how to use the API and a workaround if the API does not support the operation that you want to do. The first part of the demo shows how to start AutoCad and load a drawing. The second part shows how you can use the API to hide a layer in the drawing. The last part demonstrates how to make all groups in a drawing selectable by sending commands to the AutoCad command line. This operation can't be done with the API because there aren't any ”Selectable” property for Groups (_acadApplication.ActiveDocument.Groups).


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.Threading;
using System.Runtime.InteropServices;

namespace TestApp
{
    class Program
    {

        private IAcadApplication _acadApplication;

        static void Main(string[] args)
        {
            Program p = new Program();
            p.LoadDrawing(@"C:\\Marcus.dwg");
            p.HideLayer("LayerX");
            p.MakeGroupsSelectable();            
        }


        // start (and wait for) autocad in a very crude way...
        public Program()
        {
            _acadApplication = new Autodesk.AutoCAD.Interop.AcadApplication();
            Thread.Sleep(10000);
            _acadApplication.Visible = true;
        }

        // Load my drawing
        private void LoadDrawing(string dwgFileName)
        {
            _acadApplication.Documents.Open(dwgFileName);
        }

        // Find the layer and hide it
        public void HideLayer(string layerName)
        {
            foreach (AcadLayer layer in _acadApplication.ActiveDocument.Layers)
            {
                if (layer.Name == layerName)
                {
                    layer.LayerOn = false;
                }
            }
        }

        // Make all groups selectable by sending command to Acad
        public void MakeGroupsSelectable()
        {
            List selectableGroupsList = new List();
            foreach (AcadGroup group in _acadApplication.ActiveDocument.Groups)
            {
                // Never mind unnamed groups
                if (group.Name.StartsWith("*"))
                    continue;
                
                // Make the group selectable
                if (!selectableGroupsList.Contains(group.Name))
                {
                    _acadApplication.ActiveDocument.SendCommand("-group\n" + "Selectable\n" + group.Name + "\nYes\n\x3\x3\x3");
                    selectableGroupsList.Add(group.Name);
                }
            }
        }

    }
}


gratis casino

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Denna webbplats använder Akismet för att minska skräppost. Lär dig hur din kommentardata bearbetas.