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

if statement problem

6 REPLIES 6
Reply
Message 1 of 7
b_nerd
161 Views, 6 Replies

if statement problem

Dim oEnt1 As AcadEntity 'user picks either line or arc
Dim oEnt2 As AcadEntity 'user picks either line or arc
Dim iPoint As Variant 'point of intersection
Dim ePoint As Variant

ThisDrawing.Utility.GetEntity oEnt1, vPick, vbCr & "Pick Entity: "
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Entity: "

'assume that there is just one and always one point of intersection
iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)

If iPoint = oEnt2.startPoint Then
ePoint = oEnt2.endPoint
End if

How come this if statement doesn't execute? Any help would be appreciated. Thanks!

Bernard
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: b_nerd

Hi,

iPoint is a variant which after being computed will be late bound to an
array.
oEnt2.endPoint is a double
Hence you are trying to match two different types of data and hence getting
an error.

--


Laurie Comerford
CADApps
www.cadapps.com.au

wrote in message news:4864182@discussion.autodesk.com...
Dim oEnt1 As AcadEntity 'user picks either line or arc
Dim oEnt2 As AcadEntity 'user picks either line or arc
Dim iPoint As Variant 'point of intersection
Dim ePoint As Variant

ThisDrawing.Utility.GetEntity oEnt1, vPick, vbCr & "Pick Entity: "
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Entity: "

'assume that there is just one and always one point of intersection
iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)

If iPoint = oEnt2.startPoint Then
ePoint = oEnt2.endPoint
End if

How come this if statement doesn't execute? Any help would be appreciated.
Thanks!

Bernard
Message 3 of 7
b_nerd
in reply to: b_nerd

I want to know how to make this IF statement work. How would I rewrite it so that the IF statement executes.

Is this a safe assumption?:
iPoint has an x, y, z value
oEnt2.startPoint has an x, y, z value

So even though they are different types, there must be a way to call the IF statement and make the appropriate comparisons. Does anyone know how this can be done?

Bernard
Message 4 of 7
Anonymous
in reply to: b_nerd

I don't know if this is the best way, but it works.

Dim ent1 As AcadEntity
Dim ent2 As AcadEntity
Dim pick As Variant
Dim ints As Variant
Dim stPt As Variant
Dim endPt As Variant

ThisDrawing.Utility.GetEntity ent1, pick, "Select line 1: "
ThisDrawing.Utility.GetEntity ent2, pick, "Select line 2: "

ints = ent1.IntersectWith(ent2, acExtendNone)
stPt = ent2.StartPoint
endPt = ent2.EndPoint

If ((ints(0) = stPt(0)) And (ints(1) = stPt(1)) And (ints(2) = stPt(2))) Or
_
((ints(0) = endPt(0)) And (ints(1) = endPt(1)) And (ints(2) = endPt(2)))
Then
Debug.Print "Found one!"
Else
Debug.Print "Nope!"
End If

wrote in message news:4864192@discussion.autodesk.com...
I want to know how to make this IF statement work. How would I rewrite it so
that the IF statement executes.

Is this a safe assumption?:
iPoint has an x, y, z value
oEnt2.startPoint has an x, y, z value

So even though they are different types, there must be a way to call the IF
statement and make the appropriate comparisons. Does anyone know how this
can be done?

Bernard
Message 5 of 7
Anonymous
in reply to: b_nerd

Hi,

Here's a slightly more sophisticated approach.

Note that when you select points with the GetPoint process you get a
variant, not a double array.

The ComparePoints function would need to be modified accordingly.

--


Laurie Comerford
CADApps
www.cadapps.com.au


Sub MyProgram()
Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double
..... Do something here
If ComparePoints(pt1, pt2, 0.0001) = True Then
.... Do something here
End If
..... Do something here
End Sub

Function ComparePoints(pt1() As Double, pt2() As Double, dTol As Double) As
Boolean
On Error GoTo ErrorHandler
Dim i As Integer
If LBound(pt1) <> LBound(pt2) And UBound(pt1) <> UBound(pt2) Then
MsgBox "Array definitions are not he same size. ComparePoints cannot
process them", vbInformation
ComparePoints = False
Exit Function
End If
For i = LBound(pt1) To UBound(pt1)
If Abs(pt1(i) - pt2(i)) > dTol Then
MsgBox "Difference between values is" & Format(Abs(pt1(0) - pt2(0)),
"0.000") & "for item " & i & " in the supplied points, vbInformation"
ComparePoints = False
Exit Function
End If
Next i
ComparePoints = True
Exit Function
ErrorHandler:
MsgBox "Unable to compare supplied data in 'ComparePoints' due to:" &
vbCrLf & _
Err.Description
ComparePoints = False
End Function ' ComparePoints


"Jeff Mishler" wrote in message
news:4864216@discussion.autodesk.com...
I don't know if this is the best way, but it works.

Dim ent1 As AcadEntity
Dim ent2 As AcadEntity
Dim pick As Variant
Dim ints As Variant
Dim stPt As Variant
Dim endPt As Variant

ThisDrawing.Utility.GetEntity ent1, pick, "Select line 1: "
ThisDrawing.Utility.GetEntity ent2, pick, "Select line 2: "

ints = ent1.IntersectWith(ent2, acExtendNone)
stPt = ent2.StartPoint
endPt = ent2.EndPoint

If ((ints(0) = stPt(0)) And (ints(1) = stPt(1)) And (ints(2) = stPt(2))) Or
_
((ints(0) = endPt(0)) And (ints(1) = endPt(1)) And (ints(2) = endPt(2)))
Then
Debug.Print "Found one!"
Else
Debug.Print "Nope!"
End If

wrote in message news:4864192@discussion.autodesk.com...
I want to know how to make this IF statement work. How would I rewrite it so
that the IF statement executes.

Is this a safe assumption?:
iPoint has an x, y, z value
oEnt2.startPoint has an x, y, z value

So even though they are different types, there must be a way to call the IF
statement and make the appropriate comparisons. Does anyone know how this
can be done?

Bernard
Message 6 of 7
b_nerd
in reply to: b_nerd

There's still problems with the IF statement and other problems with this program please help!

I'm writing a program that gets an ARC and then a LINE and checks to see if the angle between the center of arc, point of intersection, and "end point" of line is 90 or 270 degrees. However, this program works in certain cases and doesn't in others. I'm assuming that the problem is the program has difficulty identifying START/END POINTS.

Here's the code

Sub isRad()

Dim vPick As Variant 'needed for .getentity command
Dim oEnt1 As AcadEntity 'user picks either line or arc
Dim oEnt2 As AcadEntity 'user picks either line or arc
Dim iPoint As Variant 'point of intersection
Dim ePoint As Variant 'end point of user selected line
Dim cPoint As Variant 'center point of user selected arc

On Error GoTo Is_Error

'Select arc or line
ThisDrawing.Utility.GetEntity oEnt1, vPick, vbCr & "Pick Entity: "
'Highlight
oEnt1.Highlight True

'if user picks an arc then next entity must be a line
If TypeOf oEnt1 Is AcadArc Then
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Line: "
oEnt2.Highlight True

iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)
ePoint = oEnt2.endPoint

If (iPoint(0) = ePoint(0)) And (iPoint(1) = ePoint(1)) And (iPoint(2) = ePoint(2)) Then
ePoint = oEnt2.startPoint
End If

cPoint = oEnt1.Center

Call RadialCheck(iPoint, ePoint, cPoint)

'if user picks a line then next entity must be an arc
ElseIf TypeOf oEnt1 Is AcadLine Then
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Arc: "
oEnt2.Highlight True

iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)
ePoint = oEnt1.endPoint

If (iPoint(0) = ePoint(0)) And (iPoint(1) = ePoint(1)) And (iPoint(2) = ePoint(2)) Then
ePoint = oEnt1.startPoint
End If

cPoint = oEnt2.Center

Call RadialCheck(iPoint, ePoint, cPoint)

End If

GoTo No_Error

Is_Error:
MsgBox "Error!"

No_Error:
oEnt1.Highlight False
oEnt2.Highlight False

End Sub

'determines angle between three points
Private Function RadialCheck(i As Variant, e As Variant, c As Variant)

Dim sngAngle1 As Single
Dim sngAngle2 As Single

sngAngle1 = ThisDrawing.Utility.AngleFromXAxis(c, i)
sngAngle1 = 180 * (sngAngle1 / (4 * Atn(1)))

sngAngle2 = ThisDrawing.Utility.AngleFromXAxis(i, e)
sngAngle2 = 180 * (sngAngle2 / (4 * Atn(1)))

If Abs(sngAngle1 - sngAngle2) = 90 Or Abs(sngAngle1 - sngAngle2) = 270 Then
MsgBox "NO radial needed"
Else
MsgBox "RADIAL NEEDED!"
End If

End Function

Any help would be appreciated!
Message 7 of 7
Anonymous
in reply to: b_nerd

Compairing two Reals for equality is dangerous. The way the real values are
stored can make the comparison to two points yeild differant results. Use
the Point compair function Laurie provided and it will solve your problem.

John


wrote in message news:4864936@discussion.autodesk.com...
There's still problems with the IF statement and other problems with this
program please help!

I'm writing a program that gets an ARC and then a LINE and checks to see if
the angle between the center of arc, point of intersection, and "end point"
of line is 90 or 270 degrees. However, this program works in certain cases
and doesn't in others. I'm assuming that the problem is the program has
difficulty identifying START/END POINTS.

Here's the code

Sub isRad()

Dim vPick As Variant 'needed for .getentity command
Dim oEnt1 As AcadEntity 'user picks either line or arc
Dim oEnt2 As AcadEntity 'user picks either line or arc
Dim iPoint As Variant 'point of intersection
Dim ePoint As Variant 'end point of user selected line
Dim cPoint As Variant 'center point of user selected arc

On Error GoTo Is_Error

'Select arc or line
ThisDrawing.Utility.GetEntity oEnt1, vPick, vbCr & "Pick Entity: "
'Highlight
oEnt1.Highlight True

'if user picks an arc then next entity must be a line
If TypeOf oEnt1 Is AcadArc Then
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Line: "
oEnt2.Highlight True

iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)
ePoint = oEnt2.endPoint

If (iPoint(0) = ePoint(0)) And (iPoint(1) = ePoint(1)) And (iPoint(2) =
ePoint(2)) Then
ePoint = oEnt2.startPoint
End If

cPoint = oEnt1.Center

Call RadialCheck(iPoint, ePoint, cPoint)

'if user picks a line then next entity must be an arc
ElseIf TypeOf oEnt1 Is AcadLine Then
ThisDrawing.Utility.GetEntity oEnt2, vPick, vbCr & "Pick Arc: "
oEnt2.Highlight True

iPoint = oEnt1.IntersectWith(oEnt2, acExtendNone)
ePoint = oEnt1.endPoint

If (iPoint(0) = ePoint(0)) And (iPoint(1) = ePoint(1)) And (iPoint(2) =
ePoint(2)) Then
ePoint = oEnt1.startPoint
End If

cPoint = oEnt2.Center

Call RadialCheck(iPoint, ePoint, cPoint)

End If

GoTo No_Error

Is_Error:
MsgBox "Error!"

No_Error:
oEnt1.Highlight False
oEnt2.Highlight False

End Sub

'determines angle between three points
Private Function RadialCheck(i As Variant, e As Variant, c As Variant)

Dim sngAngle1 As Single
Dim sngAngle2 As Single

sngAngle1 = ThisDrawing.Utility.AngleFromXAxis(c, i)
sngAngle1 = 180 * (sngAngle1 / (4 * Atn(1)))

sngAngle2 = ThisDrawing.Utility.AngleFromXAxis(i, e)
sngAngle2 = 180 * (sngAngle2 / (4 * Atn(1)))

If Abs(sngAngle1 - sngAngle2) = 90 Or Abs(sngAngle1 - sngAngle2) = 270
Then
MsgBox "NO radial needed"
Else
MsgBox "RADIAL NEEDED!"
End If

End Function

Any help would be appreciated!

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

Post to forums  

Autodesk Design & Make Report

”Boost