.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.

Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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?
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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?
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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")
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
vbNinja 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,
class keyThumper<T> : Lazy<T>; another Swamper
I do not endorse the social media app links below![]()
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
No, I want to change the color of the layer itself, I need this for specific layers, not all of them.
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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 FunctionThis 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.
Re: AutoCAD Commands from vb.net 2010
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi
This image tells what happened
so is there any missing imports?


