.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issue with code rotating one way.

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
962 Views, 9 Replies

Issue with code rotating one way.

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]);
}

 

Good-Bad.png
 
9 REPLIES 9
Message 2 of 10
kdub_nz
in reply to: Anonymous

 

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.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 3 of 10
Anonymous
in reply to: kdub_nz

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);
Message 4 of 10
kdub_nz
in reply to: Anonymous

 

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.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 5 of 10
Anonymous
in reply to: kdub_nz

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.

Message 6 of 10
Anonymous
in reply to: kdub_nz

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?

Message 7 of 10
_gile
in reply to: Anonymous

Hi

 

The optional second parameter for the Vector3d.GetAngleTo() method (called referenceVector in the docs) is the the direction of the rotation axis. So, while working in 2D, it should always be Vector3d.ZAxis.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 10
Anonymous
in reply to: _gile

So I tried changing the command to

double angleToRotate = curveDir.GetAngleTo(Vector3d.ZAxis);

It is still having the same issue as before.

Message 9 of 10
_gile
in reply to: Anonymous

It seems to me i was talking about the second parameter...

 

If I do not misunderstand, you want to get the angle from X axis to 'curveDir' about Z axis, if so, try:

 

double angleToRotate = Vector3d.XAxis.GetAngleTo(curveDir, Vector3d.ZAxis);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 10 of 10
Anonymous
in reply to: _gile

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.

Post to forums  

Forma Design Contest


AutoCAD Beta