I might be doing something horribly wrong, but I gave this code a shot at attaching arrows and bases to a line that is offset in earlier code. It works in one direction but not the other. So I attached the method used to rotate the block, as well as the piece of code that adds the block and runs the method for the arrow side.
I am attaching the code used, as well as a screencast of the problem. Hopefully, someone can help me out.
public void Align(Curve curveId, BlockReference blockId, Point3d sweetSpot) { Database dbDatabase = curveId.Database; using (Transaction trans = dbDatabase.TransactionManager.StartTransaction()) { Vector3d curveDir = curveId.GetFirstDerivative(sweetSpot); double angleToRotate = Vector3d.YAxis.GetAngleTo(curveDir, Vector3d.XAxis); blockId.Rotation = angleToRotate; trans.Commit(); } }
Point3dCollection liveEndCollection = new Point3dCollection(); subCurv.IntersectWith(cvLiveEnd, Intersect.ExtendBoth, liveEndCollection, IntPtr.Zero, IntPtr.Zero); // 0 subCurv.IntersectWith(ptDeadEnd, Intersect.ExtendBoth, liveEndCollection, IntPtr.Zero, IntPtr.Zero); // 1 BlockTable acBlkTbl = tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId = acBlkTbl["LIVE_END"]; using (BlockReference acBlkRef = new BlockReference(liveEndCollection[0], blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec = tr.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); tr.AddNewlyCreatedDBObject(acBlkRef, true); Align(subCurv, acBlkRef, liveEndCollection[1]); }
Solved! Go to Solution.
It seems to me that your rotation is being determined by the vector direction of the selected edge line.
It should be determined by the vector direction from the first point to the second point.
You will need to rectify this.
// Called Kerry in my other life.
Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.
class keyThumper<T> : Lazy<T>; another Swamper
So, rather than:
public void Align(Curve curveId, BlockReference blockId, Point3d sweetSpot) { Database dbDatabase = curveId.Database; using (Transaction trans = dbDatabase.TransactionManager.StartTransaction()) { Vector3d curveDir = curveId.GetFirstDerivative(sweetSpot); double angleToRotate = Vector3d.YAxis.GetAngleTo(curveDir, Vector3d.XAxis); blockId.Rotation = angleToRotate; trans.Commit(); } }
So would I change the angleToRotate by flipping the vectors inside the parenthesis with the YAxis vector outside?
Like this?
double angleToRotate = curveDir.GetAngleTo(Vector3d.YAxis);
Do you know the new line startpoint ?
Do you know the new line endpoint ?
What is the vector from startpoint to endpoint. ? <<- does this indicate the rotation of the arrow ?
// Called Kerry in my other life.
Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.
class keyThumper<T> : Lazy<T>; another Swamper
In the following code:
Vector3d curveDir = curveId.GetFirstDerivative(sweetSpot);
curveId is the new line, and sweetSpot is the startpoint. I can also pull the end point from another collection of points.
The arrow point is at the start point, with the back of the arrow towards the end point. Pointing out of the start point of the curve.
Alrighty, so I tried making the following code changes:
public void AlignOpp(Curve curveId, Point3dCollection pts, BlockReference blockId, Point3d sweetSpot) { Database dbDatabase = curveId.Database; using (Transaction trans = dbDatabase.TransactionManager.StartTransaction()) { Vector3d curveDir = pts[1].GetVectorTo(pts[0]); // double angleToRotate = Vector3d.YAxis.GetAngleTo(curveDir); double angleToRotate = curveDir.GetAngleTo(Vector3d.YAxis, Vector3d.XAxis); blockId.Rotation = angleToRotate; trans.Commit(); } }
And the same error keeps popping up, any suggestions?
So I tried changing the command to
double angleToRotate = curveDir.GetAngleTo(Vector3d.ZAxis);
It is still having the same issue as before.
So with a bunch of trial and error I came up with a solution that has apparently worked.
I had to change a few things, and then create a second method that would rotate the exact opposite for the arrowheads.
// ADD BLOCK TO LIVE END // Point3dCollection liveEndCollection = new Point3dCollection(); subCurv.IntersectWith(cvLiveEnd, Intersect.ExtendBoth, liveEndCollection, IntPtr.Zero, IntPtr.Zero); // 0 subCurv.IntersectWith(ptDeadEnd, Intersect.ExtendBoth, liveEndCollection, IntPtr.Zero, IntPtr.Zero); // 1 BlockTable acBlkTbl = tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId = acBlkTbl["LIVE_END"]; using (BlockReference acBlkRef = new BlockReference(liveEndCollection[0], blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec = tr.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); tr.AddNewlyCreatedDBObject(acBlkRef, true); AlignOpp2(subCurv, liveEndCollection, acBlkRef, liveEndCollection[1]); } // ADD BLOCK TO DEAD END // Point3dCollection deadEndCollection = new Point3dCollection(); subCurv.IntersectWith(ptDeadEnd, Intersect.ExtendBoth, deadEndCollection, IntPtr.Zero, IntPtr.Zero); // 0 subCurv.IntersectWith(cvLiveEnd, Intersect.ExtendBoth, deadEndCollection, IntPtr.Zero, IntPtr.Zero); // 1 BlockTable acBlkTbl2 = tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId2 = acBlkTbl["DEAD_END"];
using (BlockReference acBlkRef2 = new BlockReference(deadEndCollection[0], blkRecId2)) { BlockTableRecord acCurSpaceBlkTblRec2 = tr.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec2.AppendEntity(acBlkRef2); tr.AddNewlyCreatedDBObject(acBlkRef2, true); if (acBlkRef2.Position == deadEndCollection[0]) AlignOpp(subCurv, deadEndCollection, acBlkRef2, deadEndCollection[0]); }
public void AlignOpp(Curve curveId, Point3dCollection pts, BlockReference blockId, Point3d sweetSpot) { Database dbDatabase = curveId.Database; using (Transaction trans = dbDatabase.TransactionManager.StartTransaction()) { Vector3d curveDir = pts[0].GetVectorTo(pts[1]); double angleToRotate = Vector3d.YAxis.GetAngleTo(curveDir, Vector3d.ZAxis); blockId.Rotation = angleToRotate; trans.Commit(); } } public void AlignOpp2(Curve curveId, Point3dCollection pts, BlockReference blockId, Point3d sweetSpot) { Database dbDatabase = curveId.Database; using (Transaction trans = dbDatabase.TransactionManager.StartTransaction()) { Vector3d curveDir = pts[1].GetVectorTo(pts[0]); double angleToRotate = Vector3d.YAxis.GetAngleTo(curveDir, Vector3d.ZAxis); blockId.Rotation = angleToRotate; trans.Commit(); } }
Can't find what you're looking for? Ask the community or share your knowledge.