Hey,
Im trying to create a program witch transforms hatches from one coordinate system to another.
While doing that im trying to recreate the loops with polylines and curve2dcollection. The code for the polyline part works as of now but i cant get the curves to work.
Any ideas for what is wrong in my code or better practices for doing this transformation?
Here is a part of my code that transforms hatches. the cmdTransformCoordinates is a function i wrote, that i know works as intended, so that is not the problem.
DoubleCollection dcol = new DoubleCollection(); Point2dCollection newpl = new Point2dCollection(); Curve2dCollection tc2d = new Curve2dCollection(); IntegerCollection ticol = new IntegerCollection(); ObjectIdCollection oidc = new ObjectIdCollection(); Hatch hatch = tr.GetObject(obj, OpenMode.ForWrite) as Hatch; Plane plane = hatch.GetPlane(); int num = hatch.NumberOfLoops.DeepClone(); for (int i = 0; i < num; i++) { HatchLoop loop = hatch.GetLoopAt(0); oidc.Clear(); dcol.Clear(); if (loop.IsPolyline) { ed.WriteMessage("\n this is a pl"); BulgeVertexCollection oldpl = loop.Polyline; newpl = new Point2dCollection(); foreach (BulgeVertex vtx in oldpl) { vtx.Vertex = cmdTransformCoordinates(vtx.Vertex, currCoordSys, destCoordSys); newpl.Add(vtx.Vertex); dcol.Add(vtx.Bulge); } hatch.RemoveLoopAt(0); hatch.AppendLoop(loop.LoopType,newpl, dcol); } else { Curve2dCollection curves = loop.Curves; tc2d.Clear(); ticol.Clear(); oidc.Clear(); foreach (Curve2d cv in curves) { LineSegment2d line2d = cv as LineSegment2d; CircularArc2d arc2d = cv as CircularArc2d; EllipticalArc2d e2d = cv as EllipticalArc2d; NurbCurve2d spline2d = cv as NurbCurve2d; if (line2d != null) { Point2d sPnt = cmdTransformCoordinates(line2d.StartPoint, currCoordSys, destCoordSys); Point2d ePnt = cmdTransformCoordinates(line2d.EndPoint, currCoordSys, destCoordSys); line2d = new LineSegment2d(sPnt, ePnt); tc2d.Add(line2d); ticol.Add(1); } else if (arc2d != null) { Point2d spnt = cmdTransformCoordinates( arc2d.StartPoint, currCoordSys, destCoordSys); Point2d epnt = cmdTransformCoordinates( arc2d.EndPoint, currCoordSys, destCoordSys); Point2d cpnt = cmdTransformCoordinates( arc2d.Center, currCoordSys, destCoordSys); if (arc2d.IsClosed() || Math.Abs(arc2d.EndAngle-arc2d.StartAngle) < 1e-5) { arc2d = new CircularArc2d(cpnt, spnt.GetDistanceTo(cpnt)); tc2d.Add(arc2d); ticol.Add( (int)Enum.Parse(typeof(HatchEdgeType), HatchEdgeType.CircularArc.ToString())); } else { Point2d tpnt = cmdTransformCoordinates(arc2d.GetSamplePoints(3)[1], currCoordSys, destCoordSys); arc2d = new CircularArc2d(spnt, tpnt, epnt); ticol.Add( (int)Enum.Parse(typeof(HatchEdgeType), HatchEdgeType.CircularArc.ToString())); tc2d.Add(arc2d); } } else if (e2d != null) { Point2d eCen= cmdTransformCoordinates(e2d.Center, currCoordSys, destCoordSys); Vector2d eMaAx = e2d.MajorAxis; Vector2d eMiAx = e2d.MinorAxis; double sa = e2d.StartAngle; double ea = e2d.EndAngle; double MaR = e2d.MajorRadius; double MiR = e2d.MinorRadius; e2d = new EllipticalArc2d(eCen, eMaAx, eMiAx, sa, ea); ticol.Add((int)Enum.Parse(typeof(HatchEdgeType), HatchEdgeType.EllipticalArc.ToString())); tc2d.Add(e2d); } } hatch.RemoveLoopAt(0); hatch.AppendLoop(loop.LoopType, tc2d, ticol); } } hatch.EvaluateHatch(false);
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
What's wrong with simply transform the Hatch entity?
static void TransformHatch(Hatch hatch, CoordinateSystem3d fromCoordSys, CoordinateSystem3d toCoordSys)
{
var xform = Matrix3d.AlignCoordinateSystem(
fromCoordSys.Origin, fromCoordSys.Xaxis, fromCoordSys.Yaxis, fromCoordSys.Zaxis,
toCoordSys.Origin, toCoordSys.Xaxis, toCoordSys.Yaxis, toCoordSys.Zaxis);
hatch.TransformBy(xform);
}
Can't find what you're looking for? Ask the community or share your knowledge.