Hi
Recently I managed to connect to AutoCAD 2012 through vb.net 2010 and send simple commands like this:
Dim acaddoc As AcadDocument = acApp.ActiveDocument Dim pt1(2) As Double Dim pt2(2) As Double pt1(0) = 3 pt1(1) = 6 pt1(2) = 0 pt2(0) = 1 pt2(1) = 5 pt2(2) = 0 acaddoc.ModelSpace.AddLine(pt1, pt2)
I need to use more commands to create a new layer, set a specific layer as active, select a layer's color, select current drawing color,use hatch, select linetype and lineweight. is there a way to perform the previous operations using a code that is similar to the one above?
Solved! Go to Solution.
Solved by Mike.Wohletz. Go to Solution.
Short answer, yes.
to create the Hatch, you just call AddHatch the same way you called AddLine.
The AcadHatch object returned from that has Layer, Linetype, Lineweight properties (among others).
You access the collection of layers with acaddoc.Database.Layers which returns an object of AcadLayers, which has an Add method.
acaddoc.GetVariable/SetVariable will get or set the value of AutoCAD system variables, such as CLAYER for the name of the current layer, or CECOLOR is the Current Entity Color.
Chew that up and spit it out, then come back if you have more questions.
Hi
Sorry for being late but I've been busy the last couple of days, but I have a aquestion
the acaddoc.SetVariable function takes two parameters: Name as string, Value as object
how to use those parameters to perform tasks such as setting a layer as active or changing the layer's color?
Hi
I tried to use GetVariable func tion like this:
Dim acdoc As AcadDocument = acApp.ActiveDocument MsgBox(acaddoc.GetVariable("CECOLOR"))
And the result is a message box saying: BYLAYER
so I tried reversing this by trying the following:
acaddoc.SetVariable("CECOLOR", Common.AcColor.acByLayer)
the result is an exception saying: "Error setting system variable"
and I tried many other AutoCAD variables, the getvariable works but the setvariable doesn't. what is wrong?
The acByLayer is the same as 256 and the help says that you can use 1 to 255. You can set this with a string and make things simple by using "YELLOW", "BLUE", BLACK", "BYLAYER", "BYBLOCK", etc.
Try this:
acaddoc.SetVariable("CECOLOR", "BYLAYER")
Hi
It worked! thank you so much, I have another question please: How to change the layer's color after adding it? because I don't want to change the CEColor each time I change the current layer.
@Anonymous wrote:Hi
It worked! thank you so much, I have another question please: How to change the layer's color after adding it? because I don't want to change the CEColor each time I change the current layer.
You may need to re-state the question.
I'm not sure that you will want to change the color of the layer, because that will affect everything that is drawn on that layer that is not explicitly by color ... do you mean 'change the color of the entity' instead,
Regards,
// Called Kerry in my other life.
Everything will work just as you expect it to, unless your expectations are incorrect.
class keyThumper<T> : Lazy<T>; another Swamper
He here a function :
Public Function CreationCalque(ByVal NomCalque As String, Optional ByVal CB As Integer = 256, _ Optional ByVal CR As Integer = 256, _ Optional ByVal CV As Integer = 256) As String Dim strReturn As String = "" Dim db As Database = HostApplicationServices.WorkingDatabase Dim LayerT As LayerTable 'coll des Def des calques Dim Layer As LayerTableRecord Dim tr As Transaction = db.TransactionManager.StartTransaction() Try LayerT = tr.GetObject(db.LayerTableId, OpenMode.ForRead) If LayerT.Has(NomCalque) = False Then Layer = New LayerTableRecord Layer.Name = NomCalque Layer.Color = Color.FromRgb(CR, CV, CB) 'OpenMode.Write LayerT.UpgradeOpen() 'ajout à la base des layers LayerT.Add(Layer) 'transaction tr.AddNewlyCreatedDBObject(Layer, True) Else Layer = tr.GetObject(LayerT.Item(NomCalque), OpenMode.ForWrite) 'maJ Color Layer.Color = Color.FromRgb(CR, CV, CB) End If strReturn = Layer.Name Catch ex As Exception MsgBox(ex.ToString, vbOKOnly, "Error CreationCalque") Finally tr.Commit() tr.Dispose() End Try Return strReturn End Function
This function check if the name Layer is found otherwise it create a layer with the name pass in the function.
You need : A name of layer + code color example :
if CreationCalque("NameCalque",255,0,0) <> "" then
' calc Ok and Color applied"
' ....
end if.
Add these lines to the top of the class
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Colors
and add the reference to
acdbmgr.dll
acmgd.dll
and set copy local to false.
What namespaces have you imported? Is this a standalone EXE application?
the above code won't work for a stand alone exe.
Hi
I have the following imports:
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
And yes this is a standalone EXE Application
Hi
I have the following imports:
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
And yes this is a standalone EXE application
Try something like this to set the layer colors:
Private Sub WorkingWithAutoCAD() ' we can set the color of one layer or current by: acSetLayerColor(AcColor.acYellow) 'or 'set them all to green For Each oLayer As AcadLayer In ThisApplication.ActiveDocument.Layers acSetLayerColor(AcColor.acGreen, oLayer) Next ' or set the OB layer to red and see if it happened calling by name Dim Rval As Boolean = acSetLayerColorByName(AcColor.acRed, "OB") End Sub
Private Function acSetLayerColor(ByVal oColor As AcColor, Optional ByVal oLayer As AcadLayer = Nothing) As Boolean Try If oLayer Is Nothing Then 'we will look at the current layer oLayer = ThisApplication.ActiveDocument.ActiveLayer End If oLayer.color = oColor Return True Catch ex As Exception Return False End Try End Function
Private Function acSetLayerColorByName(ByVal oColor As AcColor, ByVal oLayerName As String) As Boolean Try Dim oLayer As AcadLayer = ThisApplication.ActiveDocument.Layers.Item(oLayerName) oLayer.color = oColor Return True Catch ex As Exception Return False End Try End Function
Hi
Your suggestions worked after I added a reference to acdbmgd.dll and acdmgd.dll and imported these namespaces:
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Colors
Thank you all for your help.
Best regards
Can't find what you're looking for? Ask the community or share your knowledge.