DrawableOverrule - making a line look like a rectangle

DrawableOverrule - making a line look like a rectangle

BKSpurgeon
Collaborator Collaborator
882 Views
1 Reply
Message 1 of 2

DrawableOverrule - making a line look like a rectangle

BKSpurgeon
Collaborator
Collaborator
  • I have a simple line overrule - basically created from this post https://www.keanw.com/2009/04/optimized-overruling-in-autocad-2010-using-net.html
  • In the original code sample, I wanted to change a line so that it looks like a cylinder. This works perfectly. The circular profile is drawn so that lines now look like cylinders.
  • What I now want to do is to use a different profile: instead of extruding a circle along a line, I now want to extrude a flat rectangle etc. (or other such shapes created from polylines) along lines. How can I do this?
  • I had originally done this using solid3ds, but ran into performance issues when there were more than 1000 lines which required overruling.

Here is my code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Colors;

namespace SampleOverrule
{
    // Review this post
    // http://through-the-interface.typepad.com/through_the_interface/2009/04/optimized-overruling-in-autocad-2010-using-net.html
    class LineOverrule : DrawableOverrule
    {
        SweepOptions sweepOptions = new SweepOptions();

        public LineOverrule()
        {
        }

        public override bool WorldDraw(Drawable drawable, WorldDraw wd)
        {
            Line line = drawable as Line;            

            if (line != null && line.Length > 0.0)
            {
                using (Autodesk.AutoCAD.DatabaseServices.Polyline rectangle = getRectangle(line))
                {
                    using (ExtrudedSurface extrusion = new ExtrudedSurface())
                    {
                        try
                        {
                            //// the circle works, no problem.
                            // Circle circle = new Circle(line.StartPoint, line.EndPoint - line.StartPoint, 50);
                            // extrusion.CreateExtrudedSurface(circle, line.EndPoint - line.StartPoint, sweepOptions);
                            
                            //// but how can one get the rectangle to work?
                            //// an exception occurs below here:
                            extrusion.CreateExtrudedSurface(rectangle, line.EndPoint - line.StartPoint, sweepOptions);

                            extrusion.WorldDraw(wd);

                            // circle.Dispose();
                        }
                        catch (System.Exception ex)
                        {

                            
                        }
                    }
                }
            }

            return base.WorldDraw(drawable, wd);
        }        

        private Autodesk.AutoCAD.DatabaseServices.Polyline getRectangle(Line line)
        {
            Autodesk.AutoCAD.DatabaseServices.Polyline polyline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
            Point2d basePointForPolyline = new Point2d(0, 0);
            polyline.AddVertexAt(0, basePointForPolyline, 0, 0, 0);
            polyline.AddVertexAt(1, new Point2d(100, 0), 0, 0, 0);
            polyline.AddVertexAt(2, new Point2d(100, 100), 0, 0, 0);
            polyline.AddVertexAt(3, new Point2d(0, 100), 0, 0, 0);
            polyline.Closed = true;

            // how do I manipulate the polyline so that its normal is rotated/moved/transformed so that
            // it aligns with the direction of the inputted line, which is passed as a parameter?
            // i.e. ideally there would be a method which does something like this: 
            // polyline.SetNewNormal(line.Delta);

            // presumably we will have to rotate and transform the polyline manually in order to do that?

            return polyline;
        }        
    }
}


//// And here is the command class

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

namespace SampleOverrule
{
    public class Commands
    {
        [CommandMethod("OverruleLines")]
        public void OverruleLines()
        {
            ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(Line)), new LineOverrule(), true);                       
        }
    }
}


// Please note, you will have to netload and run the command: OverruleLines to see the overrule working



Any advice on how one and extrude a flat polyline along the line will be much appreciated, especially that which solves performance issues when there are a large number of lines involved.

 

 

0 Likes
883 Views
1 Reply
Reply (1)
Message 2 of 2

deepak.a.s.nadig
Alumni
Alumni

Modified the WorldDraw from your code as below :

 

        public override bool WorldDraw(Drawable drawable, WorldDraw wd)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Line line = drawable as Line;

            if (line != null && line.Length > 0.0)
            {
                //using (Autodesk.AutoCAD.DatabaseServices.Polyline rectangle = getRectangle(line))
                //{
                using (ExtrudedSurface extrusion = new ExtrudedSurface())
                {
                    try
                    {
                        //// the circle works, no problem.
                        //Circle circle = new Circle(line.StartPoint, line.EndPoint - line.StartPoint, 50);
                        //extrusion.CreateExtrudedSurface(circle, line.EndPoint - line.StartPoint, sweepOptions);

                        //// but how can one get the rectangle to work?
                        //// an exception occurs below here:
                        
                        Autodesk.AutoCAD.DatabaseServices.Polyline polyline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
         
                        double x1 = line.StartPoint.X;
                        double y1 = line.StartPoint.Y;

                        polyline.AddVertexAt(0, new Point2d(x1, y1), 0.0, 0.0, 0.0);
                        polyline.AddVertexAt(1, new Point2d(x1+50, y1), 0.0, 0.0, 0.0);
                        polyline.AddVertexAt(2, new Point2d(x1+50, y1+50), 0.0, 0.0, 0.0);
                        polyline.AddVertexAt(3, new Point2d(x1, y1+50), 0.0, 0.0, 0.0);

                        polyline.Closed = true;
                        polyline.Normal = (line.EndPoint - line.StartPoint).TransformBy(ed.CurrentUserCoordinateSystem);
                        extrusion.CreateExtrudedSurface(polyline, (line.EndPoint - line.StartPoint).TransformBy(ed.CurrentUserCoordinateSystem), sweepOptions);

                        extrusion.WorldDraw(wd);                        
                        polyline.Dispose();
                        // circle.Dispose();             

                    }
                    catch (System.Exception ex)
                    {


                    }
                }
                //}
            }
            return base.WorldDraw(drawable, wd);
        }

 

 

Does this help solve your issue ?

0 Likes