.NET

Reply
Active Contributor
adjg13
Posts: 30
Registered: ‎07-10-2013
Message 1 of 3 (150 Views)

Simple Program to gather layers and change to different layers

150 Views, 2 Replies
07-23-2013 08:08 AM

I am just starting to learn C# and .NET and I am trying to write a simple program to gather all the layers from a drawing in a directory, save them to a list, and use that list to change those layers to the appropriate layers. The way I want it to iterate is open the first drawing, gather the layers, ask the user which layer they want to change the original layer to, and when they select the new layer, it associates that old layer with the new layer so that in the future it won't prompt the user, it will just change it to that layer. Then it changes the layers and goes to the next drawing.

 

I wrote something very simple in LISP, but I need to make it more advanced and try to add in more features. Specifically I would like to be able to do everything in the background, without actually opening the drawings so I can avoid any user prompts or dialogue boxes.

 

I am at a complete loss at where to start though. There are so many members in the different namespaces for Autocad. Right now I am looking through acdbmgd.dll and acmgd.dll to try and find something useful, but I have no idea where to even start.

 

If someone could suggest a good starting point that would be great. My guess would be to first define a method that gathers the layers from the drawing and saves them to an array. For example i would have multiple arrays for the different layers to which I want to convert the old layers, string [] 0layer, string [] walllayer that contains all of the layers to be changed to layer 0 and wall layer. Then I would compare the layers in the drawing to those arrays, if they are not present in any of the arrays, the program would prompt the user and ask which layers they want to change the existing layer to and add it to the appropriate drawing. Then all of the layers in those arrays would be changed to the different new layers.

 

I hope this makes some sort of sense. At the very least, I am going to start making the different arrays to be filled. That much I can do. I'm just having trouble actually getting the data from the drawings.

 

Thank you for your help.                                                                                                                              

 

Thank you.

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 2 of 3 (127 Views)

Re: Simple Program to gather layers and change to different layers

07-23-2013 09:30 PM in reply to: adjg13

Use this code as an example of how to open a DWG file for processing...

 

http://adndevblog.typepad.com/autocad/2012/07/using-readdwgfile-with-net-attachxref-or-objectarx-acd...

 

Then use this code as an example of how to iterate the layers

 

http://adndevblog.typepad.com/autocad/2012/06/locking-the-layer-through-net.html





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 3 of 3 (112 Views)

Re: Simple Program to gather layers and change to different layers

07-24-2013 02:05 AM in reply to: adjg13

Hi,

First of all your problem is not so clear to me. What I have understand from post is that first you want  iterate every layer in the drawing and put it into an array. You have some multiple arrays already and you want to compare those with your drawing layer. If any layer is missing in your drawing during comparison with your previous defined layer then you will add those layers into the drawing. if it is your problem then you can do it step by step. 1. First iterate every layer in your drawing and gather it into a array. 2. compare your drawing layers with your reference layer. 3. if any layer is missing in your drawing then add it into your drawing.

 

1. Iterate and gather layer from your drawing:

 

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

 

<CommandMethod("GL")> PublicSub GetLayer()

 

Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase= doc.Database

Dim layerList As New List(OfString)

 

Using tr AsTransaction= db.TransactionManager.StartTransaction()

Dim lt AsLayerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead)

ForEach id As ObjectId In lt

Dim ltr AsLayerTableRecord = tr.GetObject(id, OpenMode.ForRead)

layerList.Add(ltr.Name)

Next

tr.commit()

End Using

End Sub 

 

with command 'GL' I have iterate all the existing layer in the drawing and add it into a list. you can simply convert the list into an array like below.

 

Dim dwgLayer() As String = layerList .ToArray

 

2. comaparison of previous defined layer array with dwgLayer array

 

Dim defineLayer() AsString = {"LayerA", "LayerB", "LayerC", "LayerD", "LayerE"}

Dim dwgLayer() AsString = {"LayerA", "LayerE"}

Dim misMatchLayerList AsNewList(OfString)

Dim onlyDefineLayer AsIEnumerable(OfString) = defineLayer.Except(dwgLayer)

 

ForEach lyr AsStringInonlyDefineLayer

misMatchLayerList.Add(lyr)

Next

 

With the above routine you will be able to get all the layer missing in your drawing. In this case you have 3 missing layers "LayerB", "LayerC", "LayerD". you can get missing you in the misMatchLayerList. If you want you can convert the list into an arry like described in step 1. 

 

3. Add missing layer in the drawing:

 

<CommandMethod("AL")> PublicSub AddLayer()

 

Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase= doc.Database

Dim mL() AsString = {"LayerB", "LayerC", "LayerD"}

Using tr AsTransaction= db.TransactionManager.StartTransaction()

Dim lt AsLayerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead)

For i = 0 to Ubound(mL)

If lt.Has(mL(i)) = False Then

Dim ltr AsLayerTableRecord = NewLayerTableRecord()

ltr.Name = mL(i)

ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 1)

lt.UpgradeOpen()

lt.Add(ltr)

tx.AddNewlyCreatedDBObject(ltr, True)

EndIf

Next

tr.commit()

End Using

End Sub 

 

I think these codes are good starting point for you if I understand your problem correctly.

 

Regards

Zakir

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.