Hello,How to create triangle with sides 3, 5, 6 cm using CreatePolygon or one other way using VBA?Greetings?
My C# Code
[CgsAddInMacro] public Shape CreateTriangle(double a,double b, double c) { double startX = 0; double startY = 0; Point A = corelApp.Math.CreatePoint(startX, startY); Point B = corelApp.Math.CreatePoint(startX + a, startY); Shape arcA = corelApp.ActiveVirtualLayer.CreateEllipse2(A.x, A.y, c); Shape arcB = corelApp.ActiveVirtualLayer.CreateEllipse2(B.x, B.y, b,b,180,0); CrossPoints cps = null; for (int i = 1; i <= arcB.DisplayCurve.Segments.Count; i++) { for (int j = 1; j <= arcA.DisplayCurve.Segments.Count; j++) { cps = arcB.DisplayCurve.Segments[i].GetIntersections(arcA.DisplayCurve.Segments[j]); if (cps != null && cps.Count > 0) break; } if (cps != null && cps.Count > 0) break; } arcA.Delete(); arcB.Delete(); if (cps == null) throw new Exception("These measurements are not valid as a triangle"); Point C = corelApp.Math.CreatePoint(cps[1].PositionX, cps[1].PositionY); PointRange pr = corelApp.Math.CreatePointRange(); pr.AddPoint(A); pr.AddPoint(B); pr.AddPoint(C); pr.AddPoint(A); Curve curve = corelApp.CreateCurve(corelApp.ActiveDocument); curve.AppendSubpathFitToPoints(pr); Shape triangle = corelApp.ActiveLayer.CreateCurve(curve); triangle.RotationCenterX = (A.x + B.x + C.x) / 3; triangle.RotationCenterY = (A.y + B.y + C.y) / 3; return triangle; }
VBA converted by ChatGPT
Function CreateTriangle(ByVal SideA As Double, ByVal SideB As Double, ByVal SideC As Double) As ShapeDim startX As DoubleDim startY As DoublestartX = 0startY = 0
Dim A As PointSet A = ActiveDocument.Math.CreatePoint(startX, startY)
Dim B As PointSet B = ActiveDocument.Math.CreatePoint(startX + SideA, startY)
Dim arcA As ShapeSet arcA = ActiveVirtualLayer.CreateEllipse2(A.x, A.y, SideC)
Dim arcB As ShapeSet arcB = ActiveVirtualLayer.CreateEllipse2(B.x, B.y, SideB, SideB, 180, 0)
Dim cps As CrossPointsSet cps = Nothing
Dim i As IntegerDim j As IntegerFor i = 1 To arcB.DisplayCurve.Segments.CountFor j = 1 To arcA.DisplayCurve.Segments.CountSet cps = arcB.DisplayCurve.Segments(i).GetIntersections(arcA.DisplayCurve.Segments(j))If Not cps Is Nothing And cps.Count > 0 ThenExit ForEnd IfNext jIf Not cps Is Nothing And cps.Count > 0 ThenExit ForEnd IfNext i
arcA.DeletearcB.Delete
If cps Is Nothing ThenErr.Raise vbObjectError + 1, "CreateTriangle", "These measurements are not valid as a triangle"End If
Dim C As PointSet C = ActiveDocument.Math.CreatePoint(cps(1).PositionX, cps(1).PositionY)
Dim pr As PointRangeSet pr = ActiveDocument.Math.CreatePointRangepr.AddPoint Apr.AddPoint Bpr.AddPoint Cpr.AddPoint A
Dim curve As curveSet curve = CreateCurvecurve.AppendSubpathFitToPoints pr
Dim triangle As ShapeSet triangle = ActiveLayer.CreateCurve(curve)
triangle.RotationCenterX = (A.x + B.x + C.x) / 3triangle.RotationCenterY = (A.y + B.y + C.y) / 3
Set CreateTriangle = triangleEnd Function
Thank You for this answer!
Hello,Code presented from You not work for me>I found 2 errors, but they may be more1. Function CreateTriangle(ByVal a As Double, ByVal b As Double, ByVal c As Double) As ShapeDim startX As DoubleDim startY As DoublestartX = 0startY = 0Dim A As Point 'RETURN DUPLICATE DECLARATION IN CURRENT SCOPESet A = ActiveDocument.CreatePoint(startX, startY)Dim B As Point 'RETURN DUPLICATE DECLARATION IN CURRENT SCOPESet B = ActiveDocument.CreatePoint(startX + a, startY)
2. Dim arcA As ShapeSet arcA = ActiveLayer.CreateEllipse2(A.X, A.Y, C) 'RETURN INVALID QUALIFIER
Please repair and improve!
Greetings!