Urgent help req'd with code for vectors, swept angles, etc

Urgent help req'd with code for vectors, swept angles, etc

Anonymous
Not applicable
762 Views
3 Replies
Message 1 of 4

Urgent help req'd with code for vectors, swept angles, etc

Anonymous
Not applicable

Hi

 

I'm struggling with the maths needed to calculate the angle between 3 points.  My eventual aim is to use vba to draw a crescent shape polyline, ie 2 arcs curving in the same direction and joiing at their tips.  Both arcs will have different centrepoints but the same radius.  The only info I know so far is radius (R), centrepoints (CP1, CP2) and the intersection points where their end points meet (IP1, IP2).  I've gotten those points by using the 'IntersectWith' function on two circles.

 

My coding skills are fine it's just the maths I'm struggling with and after searching google for quite some time I'm still confused.  I've looked at loads of differnet formulaes and diagrams, but it always seems like they aren't complete.  Maybe it's just me.

 

Can anybody help me with this?  Any help at all would be very much appreciated 🙂

 

Cheers

 

Graham

0 Likes
Accepted solutions (1)
763 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

You can use autocad drawing tools to draw these arcs with SendCommand method. Just draw arc not by - center; start angle; end angle but by - start point; center; end point.

 

This is a interesting problem but I thinnk that you need more parameters to complete the task with math.

0 Likes
Message 3 of 4

Anonymous
Not applicable

Yeah, I don't really want to use SendCommand as it get a bit messy having to do loops and traps to ensure the SendCommand has finished before executing the next command.

 

I know what i want is possible, i just don't understand fully the calculations for vectors and dot-products (what ever that is 😞 )

0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

Right. I've struck lucky and got it working.  I can't take full credit (if any) for this as most of it is cut and paste from various help sources on the net.

 

I've posted the full code below in case it is of some use to somebody.  It's not tidied up at all yet, or even structured to fit in my existing program, but it should prove sufficient for the purposes of the subject of this topic.

 

Cheers

 

----------------------------

 

Public Const pi As Double = 3.14159265358979

Sub test()

Dim ss As AcadSelectionSet
Dim oldCircle As AcadCircle, newCircle As AcadCircle
Dim oldPt(0 To 2) As Double, newPt(0 To 2) As Double
Dim inter As Variant
Dim sp(0 To 2) As Double, ep(0 To 2) As Double
Dim OuterLoop(0 To 0) As AcadEntity

Dim hatchObj As AcadHatch

Call HatchBorder(hatchObj)

oldPt(0) = 0: oldPt(1) = 0: oldPt(2) = 0
newPt(0) = 2: newPt(1) = -2: newPt(2) = 0

For Each ss In ThisDrawing.SelectionSets
If StrComp(ss.Name, "ss", vbTextCompare) = 0 Then
ss.Delete
Exit For
End If
Next

Set ss = ThisDrawing.SelectionSets.Add("ss")
ss.SelectOnScreen

Set oldCircle = ss.Item(0)

Set newCircle = oldCircle.Copy
newCircle.Move oldPt, newPt

inter = oldCircle.IntersectWith(newCircle, acExtendNone)
Update

sp(0) = inter(0)
sp(1) = inter(1)
ep(0) = inter(3)
ep(1) = inter(4)

Call Border(inter, oldPt, newPt, OuterLoop)

Call ModifyHatch(hatchObj, OuterLoop)

End Sub

Public Sub Border(ByRef inter As Variant, oldPt() As Double, newPt() As Double, ByRef OuterLoop() As AcadEntity)

Dim coords(0 To 3) As Double
Dim i As Integer
Dim Angle As Double
Dim Bulge As Double
Dim arc1 As AcadArc
Dim arc2 As AcadArc
Dim PL As AcadLWPolyline

'set arc1 = thisdrawing.ModelSpace.addarc(oltpt, 20
coords(0) = inter(0)
coords(1) = inter(1)
coords(2) = inter(3)
coords(3) = inter(4)

'Set PL = ThisDrawing.ModelSpace.AddLightWeightPolyline(coords)
'PL.Closed = True
Set OuterLoop(0) = ThisDrawing.ModelSpace.AddLightWeightPolyline(coords)
OuterLoop(0).Closed = True
'Update
Angle = GetAngle(coords(0), coords(1), newPt(0), newPt(1), coords(2), coords(3))
Bulge = Tan((Angle * -1) / 4)

OuterLoop(0).SetBulge 0, Bulge
'PL.SetBulge 0, Bulge
'Update
Angle = (2 * pi) - Angle
Bulge = Tan(Angle / 4)

OuterLoop(0).SetBulge 1, Bulge
'PL.SetBulge 1, Bulge
Update
End Sub

Public Sub HatchBorder(hatchObj As AcadHatch)

Dim patternName As String
Dim PatternType As Long
Dim bAssociativity As Boolean

' Define the hatch
patternName = "SOLID"
PatternType = acHatchPatternTypePreDefined '0
bAssociativity = True

' Create the associative Hatch object in model space
Set hatchObj = ThisDrawing.ModelSpace.AddHatch(PatternType, patternName, bAssociativity)


End Sub

Public Sub ModifyHatch(hatchObj As AcadHatch, ByRef OuterLoop() As AcadEntity)

' Append the outerboundary to the hatch object, and display the hatch
hatchObj.AppendOuterLoop OuterLoop
hatchObj.Evaluate
ThisDrawing.Regen True

End Sub

Public Function GetAngle(ByVal Ax As Double, ByVal Ay As Double, ByVal Bx As Double, ByVal By As Double, ByVal _
Cx As Double, ByVal Cy As Double) As Double

Dim side_a As Double
Dim side_b As Double
Dim side_c As Double

' Get the lengths of the triangle's sides.
side_a = Sqr((Bx - Cx) ^ 2 + (By - Cy) ^ 2)
side_b = Sqr((Ax - Cx) ^ 2 + (Ay - Cy) ^ 2)
side_c = Sqr((Ax - Bx) ^ 2 + (Ay - By) ^ 2)

' Calculate angle B between sides ab and bc.
GetAngle = Acos((side_b ^ 2 - side_a ^ 2 - side_c ^ 2) _
/ (-2 * side_a * side_c))
End Function

' Return the arccosine of X.
Function Acos(ByVal X As Double) As Double
Acos = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
End Function

0 Likes