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
Geometry 6th grade of secondary school
Sub CreateTriangle_3_5_6cm() Dim s As Shape, s1 As Shape, s2 As Shape, cpR As CrossPoints Dim a As Double, b As Double, c As Double, X As Double, Y As Double ActiveDocument.Unit = cdrMillimeter a = 60 b = 50 c = 30 Set s = ActiveLayer.CreateLineSegment(50, 50, 50 + a, 50) Set s1 = ActiveLayer.CreateEllipse2(100, 100, b, b) s1.ConvertToCurves Set s2 = ActiveLayer.CreateEllipse2(100, 100, c, c) s2.ConvertToCurves s1.CenterX = s.Curve.Nodes(1).PositionX s1.CenterY = s.Curve.Nodes(1).PositionY s2.CenterX = s.Curve.Nodes(2).PositionX s2.CenterY = s.Curve.Nodes(2).PositionY
Set cpR = s1.Curve.SubPaths(1).GetIntersections(s2.Curve.SubPaths(1), cdrAbsoluteSegmentOffset) X = cpR.Item(2).PositionX Y = cpR.Item(2).PositionY s1.Delete s2.Delete Set s1 = ActiveLayer.CreateLineSegment(s.Curve.Nodes(1).PositionX, s.Curve.Nodes(1).PositionY, X, Y) Set s2 = ActiveLayer.CreateLineSegment(s.Curve.Nodes(2).PositionX, s.Curve.Nodes(2).PositionY, X, Y)End SubGet in touch if you need anythingBest regardsTaras
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!