Author Topic: How can I bend/trim/round an existing corner of a polyline in c# ?  (Read 7324 times)

0 Members and 1 Guest are viewing this topic.

troga

  • Guest
Hi, i´m just starting with .NET and AutoCAD playing... so first sorry for any, 'stupid' question...

my task is that I should round up the corners of an existing polyline. Under specific Angles, there
must be a pre-defined radius that should be inserted instead of the 'corner'.

So I would appreciate if someboy could give me a hint or a small peace of rope to climb up a bit out of that swamp...

1. Question... when i draw something with 'pline', there is a display of the angle where I´m moving...
    -> is there a way to get this information via API/NET ?

2. Question... is there a function/command that could be used for, let´s say 'trimming' the corner in that way I described
    above and passing points/vertices etc. as rgument or do I have to split up the polyline into segments and draw an arc by
    at the specific location by hand (code) ?

Sorry for my english, I hope the text is making sence  :oops:
(code snippets would be appreciated)

Thanks in advance.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #1 on: March 25, 2009, 12:24:36 PM »
Hi, i´m just starting with .NET and AutoCAD playing... so first sorry for any, 'stupid' question...

my task is that I should round up the corners of an existing polyline. Under specific Angles, there
must be a pre-defined radius that should be inserted instead of the 'corner'.

So I would appreciate if someboy could give me a hint or a small peace of rope to climb up a bit out of that swamp...

1. Question... when i draw something with 'pline', there is a display of the angle where I´m moving...
    -> is there a way to get this information via API/NET ?

2. Question... is there a function/command that could be used for, let´s say 'trimming' the corner in that way I described
    above and passing points/vertices etc. as rgument or do I have to split up the polyline into segments and draw an arc by
    at the specific location by hand (code) ?

Sorry for my english, I hope the text is making sence  :oops:
(code snippets would be appreciated)

Thanks in advance.
First post here? Welcome!
1. There are ways to determine the angle between two segments of a pline. I think GetAngleTo should do it.
Autodesk.AutoCAD.Geometry.Vector3d.GetAngleTo(Autodesk.AutoCAD.Geometry.Vector3d, Autodesk.AutoCAD.Geometry.Vector3d)

2. There is no way that I know of round a corner (like you would with the "fillet" command in AutoCAD) without adding a vertex to the polyline and then setting the "bulge".
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

troga

  • Guest
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #2 on: March 25, 2009, 04:08:47 PM »
Hi, i´m just starting with .NET and AutoCAD playing... so first sorry for any, 'stupid' question...

my task is that I should round up the corners of an existing polyline. Under specific Angles, there
must be a pre-defined radius that should be inserted instead of the 'corner'.

So I would appreciate if someboy could give me a hint or a small peace of rope to climb up a bit out of that swamp...

1. Question... when i draw something with 'pline', there is a display of the angle where I´m moving...
    -> is there a way to get this information via API/NET ?

2. Question... is there a function/command that could be used for, let´s say 'trimming' the corner in that way I described
    above and passing points/vertices etc. as rgument or do I have to split up the polyline into segments and draw an arc by
    at the specific location by hand (code) ?

Sorry for my english, I hope the text is making sence  :oops:
(code snippets would be appreciated)

Thanks in advance.
First post here? Welcome!
1. There are ways to determine the angle between two segments of a pline. I think GetAngleTo should do it.
Autodesk.AutoCAD.Geometry.Vector3d.GetAngleTo(Autodesk.AutoCAD.Geometry.Vector3d, Autodesk.AutoCAD.Geometry.Vector3d)

2. There is no way that I know of round a corner (like you would with the "fillet" command in AutoCAD) without adding a vertex to the polyline and then setting the "bulge".

Hi, yes it´s my first post, hopefully not the last one.
Thank you for the fast reply...
I´ll try what you suggested, give me some time to play around a little, I´m just starting with all that...

cheers.

troga

  • Guest
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #3 on: March 29, 2009, 07:06:37 AM »
Hi, i´m just starting with .NET and AutoCAD playing... so first sorry for any, 'stupid' question...

my task is that I should round up the corners of an existing polyline. Under specific Angles, there
must be a pre-defined radius that should be inserted instead of the 'corner'.

So I would appreciate if someboy could give me a hint or a small peace of rope to climb up a bit out of that swamp...

1. Question... when i draw something with 'pline', there is a display of the angle where I´m moving...
    -> is there a way to get this information via API/NET ?

2. Question... is there a function/command that could be used for, let´s say 'trimming' the corner in that way I described
    above and passing points/vertices etc. as rgument or do I have to split up the polyline into segments and draw an arc by
    at the specific location by hand (code) ?

Sorry for my english, I hope the text is making sence  :oops:
(code snippets would be appreciated)

Thanks in advance.
First post here? Welcome!
1. There are ways to determine the angle between two segments of a pline. I think GetAngleTo should do it.
Autodesk.AutoCAD.Geometry.Vector3d.GetAngleTo(Autodesk.AutoCAD.Geometry.Vector3d, Autodesk.AutoCAD.Geometry.Vector3d)

2. There is no way that I know of round a corner (like you would with the "fillet" command in AutoCAD) without adding a vertex to the polyline and then setting the "bulge".

Hi, yes it´s my first post, hopefully not the last one.
Thank you for the fast reply...
I´ll try what you suggested, give me some time to play around a little, I´m just starting with all that...

cheers.


Hi again,

ok, the angle thing worked fine, but I´m strugeling with the bulge... I´m having trouble finding out the correct pt´s
for applying the bulge, if the polyline is not closed, how can I find out if I have to add or sub a length of the current vertex ?
Does anybody has some code snippet or similar? Any help is appreciated...

Thanks in advance.

troga

  • Guest
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #4 on: April 22, 2009, 07:02:34 AM »
I have the solution... if there is interest, I´m going to share the findings...

cheers.

wannabe

  • Guest
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #5 on: April 22, 2009, 12:28:40 PM »
Please do.

troga

  • Guest
Re: How can I bend/trim/round an existing corner of a polyline in c# ?
« Reply #6 on: May 01, 2009, 04:15:21 PM »
Please do.

Ok, I have a PointCollection, of any type ... Point2d Point3d double[]... any collection type is possible, I coded this
routine independetly of the Datatype... basic double[] for independent usage... change it for your purposes.

1. the part for creating the new two points for the bulge...
Code: [Select]
public bool Create2NewPts(double[] p1, double[] p2, double[] p3,
        double radius, out double[] midP, out double[] f1, out double[] f2)
    {
        midP=new double[2];
        f1=new double[2];
        f2=new double[2];
        // direction is : 1 == left, -1 == right
        int direction=System.Math.Sign(getLinesAngle(p1,p2,p3));
        if(direction==0)
        {
            return false;
        }
   
        // vector length 1:
        double l1Length=System.Math.Sqrt(System.Math.Pow(p2[0]-p1[0],2)+System.Math.Pow(p2[1]-p1[1],2));
        if(l1Length==0)
        {
            return false;
        }
   
        // normalized vector 1:
        double[] l1nVector=new double[2];
        l1nVector[0]=-(p2[1]-p1[1])/l1Length*direction*radius;
        l1nVector[1]=(p2[0]-p1[0])/l1Length*direction*radius;
   
        // vector length 2...
        double l2Length=System.Math.Sqrt(System.Math.Pow(p3[0]-p2[0],2)+System.Math.Pow(p3[1]-p2[1],2));
        if(l2Length==0)
        {
            return false;
        }
   
        // normalized vector 2:
        double[] l2nVector=new double[2];
        l2nVector[0]=-(p3[1]-p2[1])/l2Length*direction*radius;
        l2nVector[1]=(p3[0]-p2[0])/l2Length*direction*radius;
   
        double[] l1=new double[2] { p1[0]+l1nVector[0],p1[1]+l1nVector[1] };
        double[] l2=new double[2] { p2[0]+l1nVector[0],p2[1]+l1nVector[1] };
        double[] l3=new double[2] { p2[0]+l2nVector[0],p2[1]+l2nVector[1] };
        double[] l4=new double[2] { p3[0]+l2nVector[0],p3[1]+l2nVector[1] };

        // are the lines parallel
        if((l4[0]-l3[0])/(l2[0]-l1[0])*(l2[1]-l1[1])-l4[1]+l3[1]==0)
        {
            return false;
        }
   
        double y=(l3[1]-l1[1]-(l3[0]-l1[0])/(l2[0]-l1[0])*(l2[1]-l1[1]))/((l4[0]-l3[0])/(l2[0]-l1[0])*(l2[1]-l1[1])-l4[1]+l3[1]);
   
        midP[0]=l3[0]+y*(l4[0]-l3[0]);
        midP[1]=l3[1]+y*(l4[1]-l3[1]);
        // test
        if(l2[0]==l1[0])
        {
            return false;
        }
   
        double x=(l3[0]-l1[0]+y*(l4[0]-l3[0]))/(l2[0]-l1[0]);
   
        double[] midP1=new double[2];
        midP1[0]=l1[0]+x*(l2[0]-l1[0]);
        midP1[1]=l1[1]+x*(l2[1]-l1[1]);
   
        f1[0]=p1[0]+x*(p2[0]-p1[0]);
        f1[1]=p1[1]+x*(p2[1]-p1[1]);
        f2[0]=p2[0]+y*(p3[0]-p2[0]);
        f2[1]=p2[1]+y*(p3[1]-p2[1]);
   
        return true;
    }

2. you will need to check if the method retures false... if so, you hav an angle of 90° or the angle is zero... and this means you need
    a different handling.

3. you could do all of that in a loop ... example:

Code: [Select]
            int i=0, j=0;
            for(i=0; i<=havingpoints.Count-3; i++)
            {
                Create2NewPoints(points[i],points[i+1],points[i+2]);
            }
           
            AcIn.Common.AcadPolyline polyline = doc.ModelSpace.AddPolyline(mainpoints.ToArray());
            for(i=1, j=0; i<=mainpoints.Count-3&&j<mainbulgelist.Count;i+= 2, j++)
            {
                pl.SetBulge(i,mainbulgelist[j]);
            }


where mainpoints and mainbulgelist are DoubleCollections, holding all points and all bulges.

hopefully that helps anyone else... had a hard time to get this done ...
If someone has any improvement suggestions, please do so...

cheers