Consulting

Results 1 to 5 of 5

Thread: Activate layers in AutoCAD 2015 from Excel before adding texts

  1. #1

    Activate layers in AutoCAD 2015 from Excel before adding texts

    I am working on AutoCAD 2015 & Excel 2013. I want to create texts in the drawing derived from the excel and it should go to the particular layer. I have the drawing template which has number of layers and i want to add multiple texts which should go to the respective layers defined in the excel. how can i activate a respective layer before adding a line or text in the program? i have the code for adding the multiple texts from Excel to AutoCAD, just need a code for activation of layers.
    I have created a table in excel which has the x,y, & z co-ordinates, Layer name and texts in the columns. Below is the table created. Your help is highly appreciated.

    X Y Z Text Layer
    10 10 0 Materials Layer1
    10 20 0 Bearings Layer2
    10 30 0 Square Layer3
    10 40 0 Fasteners Layer4
    10 50 0 Plates Layer5
    10 60 0 Cubes Layer6
    10 70 0 Rectangles Layer7


    Thanks in advance.

    Regards,
    Vishal
    Last edited by vishaltmahaj; 06-19-2015 at 02:31 AM. Reason: Text in the table changed..

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    This should get you where you want to go. Let me know if there is anything else.
    Sub testit()'10  10  0   "Materials"   "Layer1"
    '10  20  0   Bearings    Layer2
    '10  30  0   Square  Layer3
    '10  40  0   Fasteners   Layer4
    '10  50  0   Plates  Layer5
    '10  60  0   Cubes   Layer6
    '10  70  0   Rectangles  Layer7
    PlaceText "Materials", 10, 10, 0, "Layer1"
    PlaceText "Bearings", 10, 20, 0, "Layer2"
    PlaceText "Square", 10, 30, 0, "Layer3"
    
    
    End Sub
    
    Sub PlaceText(ByVal iText As String, ByVal iX As Double, ByVal iY As Double, ByVal iZ As Double, ByVal iLayer As String, Optional ByVal iHeight As Double = 0.125)
        Dim mPoint As Variant, mPt(0 To 2) As Double, mText As AcadText
        mPt(0) = iX: mPt(1) = iY: mPt(2) = iZ
        mPoint = mPt
        Set mText = ThisDrawing.ModelSpace.AddText(iText, mPoint, iHeight)
        With mText
            .Layer = iLayer
        End With
    End Sub

  3. #3
    Thank you Tommy.....I think I didn't explain the case in my last post....below is more explaination...Let me know if anything needed...
    I want to add text in the excel and that text has to be exported to AutoCAD in a specified layer in the excel. I would just edit the excel everytime and the code is attached to a button when I click on the button the text will be exported to AutoCAD. Number of rows in excel can be increased or decreased with new text everytime. Below is part of the code, where I want to activate the layer before adding the text.

    With Sheets("Coordinates")
            For i = 2 To LastRow
                If IsEmpty(.Range("E" & i)) = False Then
                    'Set the insertion point.
                    InsertionPoint(0) = .Range("A" & i).Value
                    InsertionPoint(1) = .Range("B" & i).Value
                    InsertionPoint(2) = .Range("C" & i).Value
                                           
                    'Add the text.
                    Set acadText = acadDoc.ModelSpace.AddText(.Range("E" & i), InsertionPoint, .Range("D" & i))
                   'Align the text based on alignment selection. The default is left.
                    If IsEmpty(.Range("F" & i)) = False Then
                        If UCase(.Range("F" & i)) = "CENTER" Then
                            acadText.Alignment = 1
                            acadText.Move ZeroPoint, InsertionPoint
                        End If
                        If UCase(.Range("F" & i)) = "RIGHT" Then
                            acadText.Alignment = 2
                            acadText.Move ZeroPoint, InsertionPoint
                        End If
                    End If
                     End If
                
            Next i
        End With
    Last edited by Tommy; 06-23-2015 at 06:01 AM. Reason: Added code tags

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Since you are insisting on activating the layer instead of placing the object on the layer :
    Sub ActivateLayer(iLayer As String)    ThisDrawing.ActiveLayer = ThisDrawing.Layers(iLayer)
    End Sub
    Sub Testit()
        ActivateLayer "layer1"
    End Sub
    If you are just adding the text and nothing else (even though you can still add objects to the layer):
    With Sheets("Coordinates")    For I = 2 To LastRow
            If IsEmpty(.Range("E" & I)) = False Then
                 'Set the insertion point.
                InsertionPoint(0) = .Range("A" & I).Value
                InsertionPoint(1) = .Range("B" & I).Value
                InsertionPoint(2) = .Range("C" & I).Value
                 
                 'Add the text.
                Set AcadText = acadDoc.ModelSpace.AddText(.Range("E" & I), InsertionPoint, .Range("D" & I))
                 'Align the text based on alignment selection. The default is left.
                If IsEmpty(.Range("F" & I)) = False Then
                    If UCase(.Range("F" & I)) = "CENTER" Then
                        AcadText.Alignment = 1
                        AcadText.Move ZeroPoint, InsertionPoint
                    End If
                    If UCase(.Range("F" & I)) = "RIGHT" Then
                        AcadText.Alignment = 2
                        AcadText.Move ZeroPoint, InsertionPoint
                    End If
                End If
                '--- ADDED
                AcadText.Layer = .Range("G" & I).Value ' I have assumed that the layer is in the G column otherwise change as needed
                'END ADD
            End If
             
        Next I
    End With

  5. #5
    Thank you tommy..it works perfectly as expected......
    Thank you again...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •