Visual Basic Customization

Visual Basic Customization

Active Member
Posts: 6
Registered: ‎03-25-2009
Message 1 of 3 (513 Views)

Divide Command does not execute

513 Views, 2 Replies
02-02-2011 02:50 PM

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)
ElseIf TypeOf Entity Is AcadPolyline Then
Set PLineObj = Entity
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
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 _
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)
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.


*Expert Elite*
Posts: 997
Registered: ‎04-27-2009
Message 2 of 3 (489 Views)

Re: Divide Command does not execute

02-03-2011 10:19 AM 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.

Active Member
Posts: 6
Registered: ‎03-25-2009
Message 3 of 3 (485 Views)

Re: Divide Command does not execute

02-03-2011 12:09 PM 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.

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.