VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Joining lwpolylines with vba

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
john1951
5033 Views, 10 Replies

Joining lwpolylines with vba

Hello

I have many short lwplines on separate layers. I need to join them by layer and then add fit or spline smoothing depending on if they are closed or open.  The plines are produced automaticaly in code, so there is nothing else on the layers besides the pline.  I am using this code to create a selection set of plines for each layer.

 

'
'
'
If sset Is Nothing Then
    Set sset = ThisDrawing.SelectionSets.Add("ss1")
End If

lifiltertype(0) = 8
v(0) = "jhl_11.00_begin"

sset.Select acSelectionSetAll, , , lifiltertype, v

 

 

I think that I have to use SendCommand to do this.  I would like to know the proper syntax to Join, Fit or Spline the plines using my selection set.

 

Thank you

John

10 REPLIES 10
Message 2 of 11
bojko108
in reply to: john1951

I'm working now on creating a method for joining polylines in VBA. I think that I have to use SendCommand method to do this. So here are some tips how you can do that. Using PEDIT command with SendCommand method, gives you option to add objects to join by one of this methods: Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle. I'm using Group to join objects. First I creat Group and then I add objects to it. Then with SendCommand I join them in single polyline.

 

Dim oLWP As AcadLWPolyline
Dim i As Long
Dim var As Variant
Dim oSS() As AcadEntity
Dim oGr As AcadGroup
Set oGr = ThisDrawing.Groups.Add("QWERT")
Dim sset As AcadSelectionSet
Set sset = ThisDrawing.SelectionSets.Add("Sa1")
sset.SelectOnScreen
' here you use your selectin set

i = 0
For Each var In sset
Set oLWP = var
ReDim Preserve oSS(i)
Set oSS(i) = oLWP
i = i + 1
Next
' add objects t\o the group
  oGr.AppendItems oSS

Dim GRname As String
GRname = oGr.Name

' using SendCommand method with Group
ThisDrawing.SendCommand "_PEDIT" & vbCr & "M" & vbCr & "G" & vbCr & GRname & vbCr & vbCr & "J" & vbCr & "0.0" & vbCr & vbCr

' deleting group and selection set
oGr.Delete
sset.Delete

About FIT and SPLINE you can use again PEDIT with SendCommand method:

 

    ' FIT
    ThisDrawing.SendCommand "_PEDIT" & vbCr & "F" & vbCr & vbCr
    ' SPLINE
    ThisDrawing.SendCommand "_PEDIT" & vbCr & "S" & vbCr & vbCr

 but selecting pline to fit or spline is difficult. Easily you can find that pline is closed or not with oLWP.Closed property. But selecting them is difficult. Methods are: Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon (you can view them by write "?" in command line when command asking you to select objects). I think that if you add all closed plines in one Group and others in second you can use then first method with Group to edit plines.

 

 

Message 3 of 11
john1951
in reply to: bojko108

Hello

This is exactly what I need to know.   I never heard of Group before.  I am going to try this right now. 

 

Many thanks

John

Message 4 of 11
bojko108
in reply to: john1951

I forgot for this option. WHen you use lines or different objects the AutoCAD will ask you do you want to convert the objects to polylines. Use this variable PEDITACCEPT and set it to 1. Then the program will convert them without asking you.

Message 5 of 11
john1951
in reply to: john1951

Hello

Here is some code that seems to work, includes Join Spline and Fit

 

 

Private Sub CommandButtonSmooth_Click()
Dim sset As AcadSelectionSet
Dim v(0) As Variant
Dim lifiltertype(0) As Integer
Dim plineObj As AcadLWPolyline
Dim oLWP As AcadLWPolyline
Dim i As Long
Dim var As Variant
Dim oSS() As AcadEntity
Dim oGr As AcadGroup
Set oGr = ThisDrawing.Groups.Add("QWERT")
Set sset = Nothing
For i = 0 To ThisDrawing.SelectionSets.Count - 1
    Set sset = ThisDrawing.SelectionSets.Item(i)
    If sset.Name = "ss1" Then
        sset.Clear
        Exit For
    Else
        Set sset = Nothing
    End If
Next i
If sset Is Nothing Then
    Set sset = ThisDrawing.SelectionSets.Add("ss1")
End If
'create a selection set of all the entities on a given layer
'here they are all lw polylines
lifiltertype(0) = 8
v(0) = "jhl_9.25_begin"
sset.Select acSelectionSetAll, , , lifiltertype, v
ReDim Preserve oSS(0 To sset.Count - 1) As AcadEntity
For i = 0 To sset.Count - 1
    Set oSS(i) = sset.Item(i)
Next i
'add plines to group
oGr.AppendItems oSS
Dim GRname As String
GRname = oGr.Name
 ' using SendCommand method with Group
ThisDrawing.SendCommand "_PEDIT" & vbCr & "M" & vbCr & "G" & vbCr & GRname & vbCr & vbCr & "J" & vbCr & "0.0" & vbCr & vbCr
' deleting group and clearing selection set
oGr.Delete
sset.Clear
'start to pedit spline or fit here
Dim oGroup As AcadGroup
Set oGroup = ThisDrawing.Groups.Add("ZERO")
GRname = oGroup.Name
'select all the joined plines
sset.Select acSelectionSetAll, , , lifiltertype, v
ReDim Preserve oSS(0) As AcadEntity
For i = 0 To sset.Count - 1
    Set oLWP = sset.Item(i)
    Set oSS(0) = sset.Item(i)
    'create group with one item
    oGroup.AppendItems oSS
    If oLWP.Closed Then
        'Spline
        ThisDrawing.SendCommand "_PEDIT" & vbCr & "M" & vbCr & "G" & vbCr & GRname & vbCr & vbCr & "S" & vbCr & vbCr
    Else
        'Fit
        ThisDrawing.SendCommand "_PEDIT" & vbCr & "M" & vbCr & "G" & vbCr & GRname & vbCr & vbCr & "F" & vbCr & vbCr
    End If
    'remove the pline from the group
    oGroup.RemoveItems oSS
Next i
sset.Delete
oGroup.Delete
End Sub

 

 

 

Message 6 of 11
Automohan
in reply to: john1951

How to use this vba program, please anyone!!!

thanks
"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
Message 7 of 11
Ed.Jobe
in reply to: Automohan

The code shown has the sub name for a Button control on a form. Just change the name to something that suits you and run the code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 8 of 11
Automohan
in reply to: Ed.Jobe

Until now never use vba code, for example how to run lisp code
copy the lisp code to node pad save it to file extension .lsp, then look into the program for "defun C:test" then run the program
"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
Message 9 of 11
Ed.Jobe
in reply to: Automohan

Go to this link at AUGI and read the first post. There are some tutorials I wrote on how to get started with VBA. VBA Lesson 1 explains what you need.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 10 of 11
Automohan
in reply to: Ed.Jobe

I know this match Visual Basic as explain in the video

Can anyone show me an video that how to use that code

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
Tags (2)
Message 11 of 11
Automohan
in reply to: Automohan

attached screencast
"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost