• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 18
    Registered: ‎11-26-2007

    how to Draw/create fillet using .NET ?

    1098 Views, 8 Replies
    01-12-2010 09:51 PM
    I created a rectangle using draw line (i am wirking on Autocad 2010)
    Now I want to create 4 fillets at each corner with some radius using vb.net
    Please somebody help me please

    Thanks
    dipak Edited by: dip_nik on Jan 13, 2010 11:22 AM
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: how to Draw/create fillet using .NET ?

    01-15-2010 01:06 AM in reply to: dip_nik
    This one is a bit tricky but working for mein A2008
    (spell all prompts)
    {code}
    using System;
    using System.Text;
    using System.Collections;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.Runtime;
    using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;


    namespace CurveMod
    {

    public class CurveModify
    {

    [CommandMethod("FLT", CommandFlags.UsePickSet & CommandFlags.Redraw)]

    static public void FilletLines()
    {
    Database db = HostApplicationServices.WorkingDatabase;

    Document doc = acadApp.DocumentManager.MdiActiveDocument;

    Editor ed = doc.Editor;

    Transaction tr = db.TransactionManager.StartTransaction();

    using (tr)
    {
    try
    {
    // Prompt for the fillet radius

    PromptDoubleOptions pdo = new PromptDoubleOptions("\nEnter the fillet radius: ");
    pdo.AllowZero = false;

    pdo.AllowNegative = false;

    pdo.AllowNone = false;

    PromptDoubleResult pdr = ed.GetDouble(pdo);

    if (pdr.Status != PromptStatus.OK)

    return;

    double rad = pdr.Value;

    // Prompt for the lines to fillet

    PromptEntityOptions peo = new PromptEntityOptions("\nSelect first line:");

    PromptEntityResult per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK)

    return;

    ObjectId fid = per.ObjectId;

    peo.Message = "\nSelect second line:";

    per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK)

    return;

    ObjectId sid = per.ObjectId;

    // get entities

    Entity ent1 = tr.GetObject(fid, OpenMode.ForRead) as Entity;

    Entity ent2 = tr.GetObject(sid, OpenMode.ForRead) as Entity;
    // cast entites as lines
    Line line1 = ent1 as Line;

    Line line2 = ent2 as Line;

    Point3dCollection intpts = new Point3dCollection();
    // get intersection between lines
    line1.IntersectWith(line2, Intersect.OnBothOperands, intpts, 0, 0);

    if (intpts.Count != 1)
    {
    acadApp.ShowAlertDialog("Lines are colinear or does not intersects");

    return;
    }

    Point3d ip = intpts[0];

    Point3d midp1;

    Point3d midp2;
    // compare points
    if (!(line1.StartPoint.Equals(ip)))
    {
    line1.UpgradeOpen();

    line1.EndPoint = line1.StartPoint;

    line1.StartPoint = ip;
    }
    midp1 = line1.GetPointAtDist(rad);

    if (!(line2.StartPoint.Equals(ip)))
    {
    line2.UpgradeOpen();

    line2.EndPoint = line2.StartPoint;

    line2.StartPoint = ip;
    }
    midp2 = line2.GetPointAtDist(rad);
    // get point on bisector
    Point3d midp = new Point3d(
    (midp1.X + midp2.X) / 2.0,
    (midp1.Y + midp2.Y) / 2.0,
    (midp1.Z + midp2.Z) / 2.0);
    // get angles along lines from intersection
    double ang1 = AngleFromX(ip, midp1);

    double ang2 = AngleFromX(ip, midp2);
    // get bisector angle
    double ang = AngleFromX(ip, midp);
    // calculate angle between lines
    double angc = Math.Abs(ang2 - ang1);
    // get a half of them
    double bis = angc / 2.0;
    // calculate hypotenuse
    double hyp = rad / Math.Sin(bis);
    // calculate center point of filleting arc
    Point3d cp = PolarPoint(ip, ang, hyp);
    // calculate another leg of a triangle
    double cat = Math.Sqrt((Math.Pow(hyp, 2)) - (Math.Pow(rad, 2)));
    // calculate center point on arc
    Point3d pa = PolarPoint(ip, ang, hyp - rad);
    // calculate start point of arc
    Point3d ps = PolarPoint(ip, ang1, cat);
    // calculate end point of arc
    Point3d pe = PolarPoint(ip, ang2, cat);
    // define arc
    Arc arc = new Arc();
    // check on direction of points
    if (isLeft(midp2, ip, midp1))
    {
    arc = new Arc(cp, rad, AngleFromX(cp, pe), AngleFromX(cp, ps));

    }
    else
    {
    arc = new Arc(cp, rad, AngleFromX(cp, ps), AngleFromX(cp, pe));

    }
    // trim lines by arc
    line1.UpgradeOpen();

    line1.StartPoint = ps;

    line2.UpgradeOpen();

    line2.StartPoint = pe;

    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
    // add arc to space
    btr.AppendEntity(arc);

    tr.AddNewlyCreatedDBObject(arc, true);

    tr.Commit();
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
    acadApp.ShowAlertDialog(ex.Message);
    }
    }//dispose transaction
    }

    ///
    /// Polar point // credit to Tony Tanzillo
    ///

    ///
    ///
    ///
    ///
    public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
    {
    return new Point3d(
    basepoint.X + (distance * Math.Cos(angle)),
    basepoint.Y + (distance * Math.Sin(angle)),
    basepoint.Z);
    }
    ///
    ///
    ///

    ///
    ///
    ///
    public static double AngleFromX(Point3d pt1, Point3d pt2)
    {
    Plane ucsplane = new Plane(new Point3d(0, 0, 0), new Vector3d(0, 0, 1));

    Vector3d vec = pt2 - pt1;

    double ang = vec.AngleOnPlane(ucsplane);

    return ang;
    }
    ///
    /// isleft function (edited) // credit to Bryco
    ///

    ///
    ///
    ///
    ///
    public static bool isLeft(Point3d spt, Point3d ept, Point3d apt)
    {
    bool result = false;

    double Ans = ((ept.X - spt.X) * (apt.Y - spt.Y) -
    (apt.X - spt.X) * (ept.Y - spt.Y));

    if (Ans > 0)
    {
    result = true;
    }
    else
    {
    result = false;
    }
    return result;
    }

    }

    }
    {code}

    ~'J'~
    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Contributor
    Posts: 18
    Registered: ‎11-26-2007

    Re: how to Draw/create fillet using .NET ?

    01-17-2010 08:40 PM in reply to: dip_nik
    Hi hallex ,

    Thanks a lottttttttt
    for the reply.

    Have a nice Day
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: how to Draw/create fillet using .NET ?

    01-17-2010 10:33 PM in reply to: dip_nik
    Glad to help
    Cheers :smileyhappy:

    ~'J'~
    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    cean_au
    Posts: 99
    Registered: ‎07-11-2011

    Re: how to Draw/create fillet using .NET ?

    12-26-2011 07:56 PM in reply to: dip_nik

    is there a vb.net version?

     

    thx

    Please use plain text.
    ADN Support Specialist
    Balaji_Ram
    Posts: 358
    Registered: ‎03-21-2011

    Re: how to Draw/create fillet using .NET ?

    12-26-2011 08:19 PM in reply to: cean_au

    Hi,

    Here is a link that I personally find useful in converting code from C# to VB.Net and vice-versa.
    Some basic things will need to be fixed if you get some compile errors.

     

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

     



    Balaji
    Developer Technical Services
    Autodesk Developer Network

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: how to Draw/create fillet using .NET ?

    12-27-2011 12:52 AM in reply to: cean_au

     

     

            <CommandMethod("FilletLines", "FLT", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
            Public Shared Sub FilletLines()
                Dim db As Database = HostApplicationServices.WorkingDatabase
     
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
     
                Dim ed As Editor = doc.Editor
     
                Dim tr As Transaction = db.TransactionManager.StartTransaction()
     
                Using tr
                    Try
                        ' Prompt for the fillet radius
     
                        Dim pdo As New PromptDoubleOptions(vbLf & "Enter the fillet radius: ")
                        pdo.AllowZero = False
     
                        pdo.AllowNegative = False
     
                        pdo.AllowNone = False
     
                        Dim pdr As PromptDoubleResult = ed.GetDouble(pdo)
     
                        If pdr.Status <> PromptStatus.OK Then
     
                            Return
                        End If
     
                        Dim rad As Double = pdr.Value
     
                        ' Prompt for the lines to be filleted
     
                        Dim peo As New PromptEntityOptions(vbLf & "Select first line:")
     
                        Dim per As PromptEntityResult = ed.GetEntity(peo)
     
                        If per.Status <> PromptStatus.OK Then
     
                            Return
                        End If
     
                        Dim fid As ObjectId = per.ObjectId
     
                        peo.Message = vbLf & "Select second line:"
     
                        per = ed.GetEntity(peo)
     
                        If per.Status <> PromptStatus.OK Then
     
                            Return
                        End If
     
                        Dim sid As ObjectId = per.ObjectId
     
                        ' get entities
     
                        Dim ent1 As Entity = TryCast(tr.GetObject(fid, OpenMode.ForRead), Entity)
     
                        Dim ent2 As Entity = TryCast(tr.GetObject(sid, OpenMode.ForRead), Entity)
                        ' cast entites as lines
                        Dim line1 As Line = TryCast(ent1, Line)
     
                        Dim line2 As Line = TryCast(ent2, Line)
     
                        Dim intpts As New Point3dCollection()
                        ' get intersection between lines
                        line1.IntersectWith(line2, Intersect.OnBothOperands, intpts, 0, 0)
     
                        If intpts.Count <> 1 Then
     
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Lines are colinear or does not intersects")
     
                            Return
                        End If
     
                        Dim ip As Point3d = intpts(0)
     
                        Dim midp1 As Point3d
     
                        Dim midp2 As Point3d
                        ' compare points
                        If Not (line1.StartPoint.Equals(ip)) Then
                            line1.UpgradeOpen()
     
                            line1.EndPoint = line1.StartPoint
     
                            line1.StartPoint = ip
                        End If
                        midp1 = line1.GetPointAtDist(rad)
     
                        If Not (line2.StartPoint.Equals(ip)) Then
                            line2.UpgradeOpen()
     
                            line2.EndPoint = line2.StartPoint
     
                            line2.StartPoint = ip
                        End If
                        midp2 = line2.GetPointAtDist(rad)
                        ' get point on bisector
                        Dim midp As New Point3d((midp1.X + midp2.X) / 2.0, (midp1.Y + midp2.Y) / 2.0, (midp1.Z + midp2.Z) / 2.0)
                        ' get angles along lines from intersection
                        Dim ang1 As Double = AngleFromX(ip, midp1)
     
                        Dim ang2 As Double = AngleFromX(ip, midp2)
                        ' get bisector angle
                        Dim ang As Double = AngleFromX(ip, midp)
                        ' calculate angle between lines
                        Dim angc As Double = Math.Abs(ang2 - ang1)
                        ' get a half of them
                        Dim bis As Double = angc / 2.0
                        ' calculate hypotenuse
                        Dim hyp As Double = rad / Math.Sin(bis)
                        ' calculate center point of filleting arc
                        Dim cp As Point3d = PolarPoint(ip, ang, hyp)
                        ' calculate another leg of a triangle
                        Dim cat As Double = Math.Sqrt((Math.Pow(hyp, 2)) - (Math.Pow(rad, 2)))
                        ' calculate center point on arc
                        Dim pa As Point3d = PolarPoint(ip, ang, hyp - rad)
                        ' calculate start point of arc
                        Dim ps As Point3d = PolarPoint(ip, ang1, cat)
                        ' calculate end point of arc
                        Dim pe As Point3d = PolarPoint(ip, ang2, cat)
                        ' define arc
                        Dim arc As New Arc()
                        ' check on direction of points
                        If isLeft(midp2, ip, midp1) Then
     
                            arc = New Arc(cp, rad, AngleFromX(cp, pe), AngleFromX(cp, ps))
                        Else
     
                            arc = New Arc(cp, rad, AngleFromX(cp, ps), AngleFromX(cp, pe))
                        End If
                        ' trim lines by arc
                        line1.UpgradeOpen()
     
                        line1.StartPoint = ps
     
                        line2.UpgradeOpen()
     
                        line2.StartPoint = pe
     
                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                        ' add arc to space
                        btr.AppendEntity(arc)
     
                        tr.AddNewlyCreatedDBObject(arc, True)
     
                        tr.Commit()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message)
                    End Try
                End Using 'dispose transaction
     
            End Sub

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    cean_au
    Posts: 99
    Registered: ‎07-11-2011

    Re: how to Draw/create fillet using .NET ?

    12-29-2011 12:46 AM in reply to: Hallex

    thx Hallex.

     

               '///
                '/// Polar point // credit to Tony Tanzillo
                '///
                Public Shared Function PolarPoint(ByVal basepoint As Point3d, ByVal angle As Double, ByVal distance As Double) As Point3d

                    Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)

                End Function
                '///
                '///
                Public Shared Function AngleFromX(ByVal pt1 As Point3d, ByVal pt2 As Point3d) As Double

                    Dim ang As Double
                    Dim vec As Vector3d
                    Dim ucsplane As Plane

                    ucsplane = New Plane(New Point3d(0, 0, 0), New Vector3d(0, 0, 1))

                    vec = pt2 - pt1

                    ang = vec.AngleOnPlane(ucsplane)

                    Return ang

                End Function
                '///
                '/// isleft function (edited) // credit to Bryco
                '///
                Public Shared Function isLeft(ByVal spt As Point3d, ByVal ept As Point3d, ByVal apt As Point3d) As Boolean

                    Dim Ans As Double
                    Dim result As Boolean

                    result = False

                    Ans = ((ept.X - spt.X) * (apt.Y - spt.Y) - (apt.X - spt.X) * (ept.Y - spt.Y))

                    If (Ans > 0) Then
                        result = True
                    Else
                        result = False
                    End If

                    Return result

                End Function

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: how to Draw/create fillet using .NET ?

    12-29-2011 04:31 AM in reply to: cean_au

    You're welcome

    Cheers :smileyhappy:

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.