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

Divide Command does not execute

2 REPLIES 2
Reply
Message 1 of 3
kaz67
788 Views, 2 Replies

Divide Command does not execute

I am using the divide command and then selecting the points for a selection set.  The program has been working for years just fine.  We bought a Windows 7 machine and now the divide command is not working in VBA.  It does not execute until the function it is in is exited.

Public Function Div(Entity As AcadEntity, Segs As Integer) As Variant
Dim DXFCode(0 To 1) As Integer
Dim FilterData As Variant
Dim Handle As String
Dim Index As Integer
Dim Layer As Variant
Dim LayerExists As Boolean
Dim LineObj As AcadLine
Dim MyLayer As String
Dim PLineObj As AcadPolyline
Dim PointObj As AcadPoint
Dim ReturnAry() As Double
MyLayer = ThisDrawing.ActiveLayer.Name
ThisDrawing.SetVariable "NOMUTT", 1
' if Entity is a line then make it a polyline
If TypeOf Entity Is AcadLine Then
Set LineObj = Entity
' perform late binding required by CreateTypedArray method
Set UtilObj = ThisDrawing.Utility
UtilObj.CreateTypedArray RetVal, vbDouble, _
LineObj.StartPoint(0), LineObj.StartPoint(1), _
LineObj.StartPoint(2), LineObj.EndPoint(0), _
LineObj.EndPoint(1), LineObj.EndPoint(2)
Set UtilObj = Nothing ' free the Utility object
Entity.Delete ' erase the line entity
Set PLineObj = ThisDrawing.ModelSpace.AddPolyline(RetVal)
PLineObj.Update
ElseIf TypeOf Entity Is AcadPolyline Then
Set PLineObj = Entity
Else
MyError "DIV: Entity must be a line or a polyline"
End If
' check if the $POINTS$ layer already exists in the drawing
LayerExists = False
For Each Layer In ThisDrawing.Layers
If StrComp(Layer.Name, "&POINTS$", 1) = 0 Then
LayerExists = True
End If
Next Layer
' if the $POINTS$ layer does not exist in the drawing, create it
If LayerExists Then
ThisDrawing.Layers.Item("$POINTS$").LayerOn = True
Else
ThisDrawing.Layers.Add ("$POINTS$")
End If
' run the DIVIDE command on the polyline
ThisDrawing.SetVariable "CLAYER", "$POINTS$"
ThisDrawing.SetVariable "PDMODE", 0
ThisDrawing.SetVariable "PDSIZE", 0
Handle = PLineObj.Handle
ThisDrawing.SendCommand "DIVIDE" & vbCr & "(handent """ & _
Handle & """)" & vbCr & Segs & vbCr
ThisDrawing.SetVariable "CLAYER", MyLayer
' select all points on the $POINTS$ layer
Set SelSet = ThisDrawing.SelectionSets.Add("DivSS")
DXFCode(0) = 0 ' code for entity type
DXFCode(1) = 8 ' code for entity layer
FilterData = Array("POINT", "$POINTS$")
SelSet.Select acSelectionSetAll, , , DXFCode, FilterData
' make room for an extra point at the start of the array
ReDim ReturnAry(0 To 2) As Double
' build an array of points on the $POINTS$ layer
For Index = 0 To SelSet.Count - 1
ReDim Preserve ReturnAry(0 To UBound(ReturnAry) + 3) As _
Double
Set PointObj = SelSet.Item(Index)
ReturnAry(UBound(ReturnAry) - 2) = PointObj.Coordinates(0)
ReturnAry(UBound(ReturnAry) - 1) = PointObj.Coordinates(1)
Next Index
SelSet.Erase ' delete the points
SelSet.Delete ' delete the selection set
' make room for an extra point at the end of the array
ReDim Preserve ReturnAry(0 To UBound(ReturnAry) + 3) As Double
ThisDrawing.Layers.Item("$POINTS$").LayerOn = False
ThisDrawing.SetVariable "PDMODE", 3
ThisDrawing.SetVariable "PDSIZE", 0.5
' check to see if the points array must be reversed
If ReturnAry(3) < ReturnAry(UBound(ReturnAry) - 5) Then
RetVal = Reverse(ReturnAry, True)
For Index = 0 To UBound(ReturnAry)
ReturnAry(Index) = RetVal(Index)
Next Index
End If
' fill in the first and last points in the array
RetVal = PLineObj.Coordinates
If RetVal(0) < RetVal(UBound(RetVal) - 2) Then
ReturnAry(0) = RetVal(UBound(RetVal) - 2)
ReturnAry(1) = RetVal(UBound(RetVal) - 1)
ReturnAry(UBound(ReturnAry) - 2) = RetVal(0)
ReturnAry(UBound(ReturnAry) - 1) = RetVal(1)
Else
ReturnAry(0) = RetVal(0)
ReturnAry(1) = RetVal(1)
ReturnAry(UBound(ReturnAry) - 2) = RetVal(UBound(RetVal) - 2)
ReturnAry(UBound(ReturnAry) - 1) = RetVal(UBound(RetVal) - 1)
End If
Div = ReturnAry
ThisDrawing.SendCommand "NOMUTT" & vbCr & "0" & vbCr
End Function

 

The Div function is called from another function.  If we let the program run normally the divide command does not execute and it crashes.  If we exit the program just after the Div command is called the divide command then executes.

 

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: kaz67

Is the Win7 32-bit or 64-bit? That is, the AutoCAD installed is a 32-bit one or 64-bit one?

 

If previously the code works with 32-bit and now do not with 64-bit, then it is very much expected. IMO, even it worked before with 32-bit AutoCAD, it mostly because of the good luck you had, judging by the code using SendCommand() in the middle of long list of code.

 

Since you or someone can programming in VBA, it is fairly easy to do your own calculation mathematically and geometrically to get the same result as AutoCAD "divide" command does, you really should avoid the SendCommand() here.

 

Only use SendCommand when there is no other way to do a particular thing in Acad and only when the SendCommand() is called at the very end of code execution.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
kaz67
in reply to: norman.yuan

It is 32 bit.  The divide was probably used in the original programming because it was the path of least resistance.  They did not want to rebuild something that was already available.  That may be the route we have to take.  Thanks for the response.

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

Post to forums  

Autodesk Design & Make Report

”Boost