• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 21
    Registered: ‎03-18-2012
    Accepted Solution

    AutoCAD Commands from vb.net 2010

    562 Views, 16 Replies
    03-22-2012 10:12 AM

    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?

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: AutoCAD Commands from vb.net 2010

    03-22-2012 02:40 PM in reply to: vbNinja

    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.

    Dave O.                                                                 Sig-Logos32.png
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎03-18-2012

    Re: AutoCAD Commands from vb.net 2010

    03-24-2012 10:09 AM in reply to: chiefbraincloud

    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?

     


    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎03-18-2012

    Re: AutoCAD Commands from vb.net 2010

    03-25-2012 08:42 AM in reply to: vbNinja

    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?

     

    Please use plain text.
    Valued Mentor
    Mike.Wohletz
    Posts: 351
    Registered: ‎07-29-2008

    Re: AutoCAD Commands from vb.net 2010

    03-25-2012 10:18 AM in reply to: vbNinja

    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")
    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎03-18-2012

    Re: AutoCAD Commands from vb.net 2010

    03-25-2012 10:30 AM in reply to: Mike.Wohletz

    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.

    Please use plain text.
    Valued Mentor
    KerryBrown
    Posts: 259
    Registered: ‎11-29-2008

    Re: AutoCAD Commands from vb.net 2010

    03-25-2012 08:28 PM in reply to: vbNinja

    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:smileyembarrassed:

    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎03-18-2012

    Re: AutoCAD Commands from vb.net 2010

    03-25-2012 09:58 PM in reply to: KerryBrown

    No, I want to change the color of the layer itself, I need this for specific layers, not all of them.

    Please use plain text.
    Mentor
    Posts: 260
    Registered: ‎01-27-2010

    Re: AutoCAD Commands from vb.net 2010

    03-26-2012 03:36 AM in reply to: vbNinja

    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.

     

     

     

    Please use plain text.
    Contributor
    Posts: 21
    Registered: ‎03-18-2012

    Re: AutoCAD Commands from vb.net 2010

    03-26-2012 10:41 AM in reply to: AubelecBE

    Hi

     

    This image tells what happened

    Untitled.png

    so is there any missing imports?

    Please use plain text.