### Author Topic: Rotate grip to match direction  (Read 1201 times)

0 Members and 1 Guest are viewing this topic.

#### Jeff H

• Needs a day job
• Posts: 5042
##### Rotate grip to match direction
« on: October 11, 2012, 12:45:19 pm »
I am not sure what the correct words are how to ask the question correctly, I cheated to pass Calculus 3.

If I got segment from a polyline the LineSegment3D has a direction property how do I apply that or what do I have to do to translate it?

For example in the pic above what do i have to do with direction vector information to make it rotate with line? I know it is some math but just not coming to me.

Here is the test code thrown together to see how it worked.
Code - C#: [Select]
`   public class ConduitGripData : GripData    {      public GripType GripType { get; set; }       public override bool ViewportDraw(Autodesk.AutoCAD.GraphicsInterface.ViewportDraw worldDraw, ObjectId entityId, DrawType type, Autodesk.AutoCAD.Geometry.Point3d? imageGripPoint, int gripSizeInPixels)       {            Point2d glyphSize = worldDraw.Viewport.GetNumPixelsInUnitSquare(this.GripPoint);           Double glyphHeight = (gripSizeInPixels / glyphSize.Y);           Matrix3d e2w = worldDraw.Viewport.EyeToWorldTransform;           Point3d pnt = this.GripPoint.TransformBy(e2w);           Point3dCollection pnts = GlyphPoints(glyphHeight, pnt);           worldDraw.Geometry.DeviceContextPolygon(pnts);           return false;       }       private Point3dCollection GlyphPoints(double glyphHeight, Point3d pnt)       {           Point3dCollection pnts = new Point3dCollection();           switch (GripType)           {               case GripType.MidPoint:                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 1.5), pnt.Y + (glyphHeight / 2), pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 1.5), pnt.Y - (glyphHeight / 2), pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 1.5), pnt.Y - (glyphHeight / 2), pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 1.5), pnt.Y + (glyphHeight / 2), pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 1.5), pnt.Y + (glyphHeight / 2), pnt.Z));                   return pnts;               default:                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 1.1547), pnt.Y, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y - glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 0.57736), pnt.Y - glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 1.1547), pnt.Y, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   return pnts;           }       } `

#### Jeff H

• Needs a day job
• Posts: 5042
##### Re: Rotate grip to match direction
« Reply #1 on: October 12, 2012, 08:02:17 am »
With a sample from Balaji Ramamoorthy I was able get it working for now.

Code - C#: [Select]
`     public class ConduitGripData : GripData    {      public GripType GripType { get; set; }      public Vector3d Direction { get; set; }       public override bool ViewportDraw(Autodesk.AutoCAD.GraphicsInterface.ViewportDraw worldDraw, ObjectId entityId, DrawType type, Autodesk.AutoCAD.Geometry.Point3d? imageGripPoint, int gripSizeInPixels)       {            Point2d glyphSize = worldDraw.Viewport.GetNumPixelsInUnitSquare(this.GripPoint);           Double glyphHeight = (gripSizeInPixels / glyphSize.Y);           Matrix3d e2w = worldDraw.Viewport.EyeToWorldTransform;           Point3d pnt = this.GripPoint.TransformBy(e2w);           Point3dCollection pnts = GlyphPoints(glyphHeight, pnt, this.Direction);           worldDraw.Geometry.DeviceContextPolygon(pnts);           return false;       }       private Point3dCollection GlyphPoints(double glyphHeight, Point3d pnt, Vector3d direction)       {           Point3dCollection pnts = new Point3dCollection();           switch (GripType)           {               case GripType.MidPoint:                    Vector3d perpDirection = direction.GetPerpendicularVector();                    Matrix3d mat = Matrix3d.Displacement(direction.Negate().GetNormal() * glyphHeight * 1.5);                   mat = mat.PreMultiplyBy(Matrix3d.Displacement(perpDirection.GetNormal() * glyphHeight * 0.5));                   Point3d p1 = pnt.TransformBy(mat);                    mat = Matrix3d.Displacement(direction.Negate().GetNormal() * glyphHeight * 1.5);                             mat = mat.PreMultiplyBy(Matrix3d.Displacement(perpDirection.Negate().GetNormal() * glyphHeight * 0.5));                            Point3d p2 = pnt.TransformBy(mat);                    mat = Matrix3d.Displacement(direction.GetNormal() * glyphHeight * 1.5);                       mat = mat.PreMultiplyBy(Matrix3d.Displacement(perpDirection.Negate().GetNormal() * glyphHeight * 0.5));                    Point3d p3 = pnt.TransformBy(mat);                    mat = Matrix3d.Displacement(direction.GetNormal() * glyphHeight * 1.5);                   mat = mat.PreMultiplyBy(Matrix3d.Displacement(perpDirection.GetNormal() * glyphHeight * 0.5));                   Point3d p4 = pnt.TransformBy(mat);                    pnts.Add(p1);                   pnts.Add(p2);                   pnts.Add(p3);                   pnts.Add(p4);                   pnts.Add(p1);                   return pnts;               default:                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 1.1547), pnt.Y, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y - glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 0.57736), pnt.Y - glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 1.1547), pnt.Y, pnt.Z));                   pnts.Add(new Point3d(pnt.X - (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   pnts.Add(new Point3d(pnt.X + (glyphHeight * 0.57736), pnt.Y + glyphHeight, pnt.Z));                   return pnts;           }       }    } `

#### BlackBox

• Gator
• Posts: 3281
##### Re: Rotate grip to match direction
« Reply #2 on: October 12, 2012, 09:13:23 am »
I recently watched the first ADN DevTech video, and they went around the room speaking to participants that came to a DevDays session... One of them was adding custom grips as well, which sparked some interest for what we do here at my office.

Not really sure where to begin, so I thank you for posting the sample code here, Jeff.

Would you mind also posting a link, or file path (if part of SDK) to that sample you found useful?

TIA
"Potential has a shelf life." - Margaret Atwood

#### Jeff H

• Needs a day job
• Posts: 5042
##### Re: Rotate grip to match direction
« Reply #3 on: October 12, 2012, 09:37:19 am »
That example did not do much as was starting off getting something working and actually does not work for doing anything for the grips.

The example mentioned was a email from ADn response so I am not sure I could post it and had nothing to do with grips but was similiar to below but asked for two points and added 4 DBPoints at four corners after finding midpoint.

Here is one that might help

#### BlackBox

• Gator
• Posts: 3281
##### Re: Rotate grip to match direction
« Reply #4 on: October 12, 2012, 09:40:38 am »
That example did not do much as was starting off getting something working and actually does not work for doing anything for the grips.

The example mentioned was a email from ADn response so I am not sure I could post it and had nothing to do with grips but was similiar to below but asked for two points and added 4 DBPoints at four corners after finding midpoint.

Here is one that might help

No worries; Thanks for the link!
"Potential has a shelf life." - Margaret Atwood

#### Jeff H

• Needs a day job
• Posts: 5042
##### Re: Rotate grip to match direction
« Reply #5 on: October 12, 2012, 09:44:01 am »
There is much more to add but for starters for drawing your grip points you can use the class in previous post that inherits from GripData, and set the points that will be called in the ViewportDraw method in previous post from GetGrippoints in a class below that inherits GripOverrule

Code - C#: [Select]
`   public class ConduitGripOverrule : GripOverrule    {        public static bool OverrulesOn = false;        public static ConduitGripOverrule CndtOverrule = new ConduitGripOverrule();        public override void GetGripPoints(Entity entity, GripDataCollection grips, double curViewUnitSize, int gripSize, Vector3d curViewDir, GetGripPointsFlags bitFlags)        {            Polyline plyLine = entity as Polyline;            SegmentType previousSegmentType = SegmentType.Empty;            for (int i = 0; i < plyLine.NumberOfVertices; i++)            {                if (plyLine.GetSegmentType(i) == SegmentType.Line)                {                    LineSegment3d lseg = plyLine.GetLineSegmentAt(i);                    if (previousSegmentType != SegmentType.Line)                    {                        ConduitGripData cndtGripDataStartPoint = new ConduitGripData();                        cndtGripDataStartPoint.GripPoint = lseg.StartPoint;                        cndtGripDataStartPoint.GripType = GripType.StartPoint;                        cndtGripDataStartPoint.Direction = lseg.Direction;                        grips.Add(cndtGripDataStartPoint);                    }                    ConduitGripData cndtGripDataMidPoint = new ConduitGripData();                    cndtGripDataMidPoint.GripPoint = lseg.MidPoint;                    cndtGripDataMidPoint.GripType = GripType.MidPoint;                    cndtGripDataMidPoint.Direction = lseg.Direction;                    grips.Add(cndtGripDataMidPoint);                     ConduitGripData cndtGripDataEndPoint = new ConduitGripData();                    cndtGripDataEndPoint.GripPoint = lseg.EndPoint;                    cndtGripDataEndPoint.GripType = GripType.EndPoint;                    cndtGripDataEndPoint.Direction = lseg.Direction;                    grips.Add(cndtGripDataEndPoint);                 }                previousSegmentType = plyLine.GetSegmentType(i);            }             //base.GetGripPoints(entity, grips, curViewUnitSize, gripSize, curViewDir, bitFlags);        }        public override void MoveGripPointsAt(Entity entity, GripDataCollection grips, Vector3d offset, MoveGripPointsFlags bitFlags)        {            base.MoveGripPointsAt(entity, grips, offset, bitFlags);        }        //public override void MoveGripPointsAt(Entity entity, IntegerCollection indices, Vector3d offset)        //{        //    base.MoveGripPointsAt(entity, indices, offset);        //}    } `

#### TT

• Swamp Rat
• Posts: 826
##### Re: Rotate grip to match direction
« Reply #6 on: October 13, 2012, 10:08:09 pm »
With a sample from Balaji Ramamoorthy I was able get it working for now.

Code - C#: [Select]
`    <snip>   `

That's the example he gave you?

To merely rotate the grip, you can just push/pop the model transform
before/after supermessaging the base class's ViewportDraw method, and
there's no need to draw it yourself.

See Geometry.PushOrientation() and Geometry.PopOrientation() in the docs.

The first would be called before you call base.ViewportDraw(), and the
second would be called after it returns.

If this is a representative example of the kind of support that paying customers get, then they're getting robbed.