Public Test Forum

Reply
Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 1 of 5 (437 Views)

Code posting

437 Views, 4 Replies
03-19-2013 03:16 PM
' (C) Copyright 2011 by  
'
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput

' This line is not mandatory, but improves loading performances
<Assembly: CommandClass(GetType(AutoCAD_VB_plug_in1.MyCommands))>
Namespace AutoCAD_VB_plug_in1

    ' This class is instantiated by AutoCAD for each document when
    ' a command is called by the user the first time in the context
    ' of a given document. In other words, non static data in this class
    ' is implicitly per-document!
    Public Class MyCommands

        ' The CommandMethod attribute can be applied to any public  member
        ' function of any public class.
        ' The function should take no arguments and return nothing.
        ' If the method is an instance member then the enclosing class is
        ' instantiated for each document. If the member is a static member then
        ' the enclosing class is NOT instantiated.
        '
        ' NOTE: CommandMethod has overloads where you can provide helpid and
        ' context menu.

        ' Modal Command with localized name
        ' AutoCAD will search for a resource string with Id "MyCommandLocal" in the
        ' same namespace as this command class.
        ' If a resource string is not found, then the string "MyLocalCommand" is used
        ' as the localized command name.
        ' To view/edit the resx file defining the resource strings for this command,
        ' * click the 'Show All Files' button in the Solution Explorer;
        ' * expand the tree node for myCommands.vb;
        ' * and double click on myCommands.resx
        <CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)> _
        Public Sub MyCommand() ' This method can have any name
            ' Put your command code here
        End Sub

        ' Modal Command with pickfirst selection
        <CommandMethod("MyGroup", "MyPickFirst", "MyPickFirstLocal", CommandFlags.Modal + CommandFlags.UsePickSet)> _
        Public Sub MyPickFirst() ' This method can have any name
            Dim result As PromptSelectionResult = Application.DocumentManager.MdiActiveDocument.Editor.GetSelection()
            If (result.Status = PromptStatus.OK) Then
                ' There are selected entities
                ' Put your command using pickfirst set code here
            Else
                ' There are no selected entities
                ' Put your command code here
            End If
        End Sub

        ' Application Session Command with localized name
        <CommandMethod("MyGroup", "MySessionCmd", "MySessionCmdLocal", CommandFlags.Modal + CommandFlags.Session)> _
        Public Sub MySessionCmd() ' This method can have any name
            ' Put your command code here
        End Sub

        ' LispFunction is similar to CommandMethod but it creates a lisp
        ' callable function. Many return types are supported not just string
        ' or integer.
        <LispFunction("MyLispFunction", "MyLispFunctionLocal")> _
        Public Function MyLispFunction(ByVal args As ResultBuffer) ' This method can have any name
            ' Put your command code here

            ' Return a value to the AutoCAD Lisp Interpreter
            Return 1
        End Function

    End Class

End Namespace

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 2 of 5 (435 Views)

Re: Code posting

03-19-2013 03:22 PM in reply to: Alexander.Rivilis
  1. using System;
  2. using System.Reflection;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.EditorInput;
  8.  
  9. [assembly: CommandClass(typeof(Rivilis.HatchUtils))]
  10.  
  11. namespace Rivilis
  12. {
  13.   public class HatchUtils
  14.   {
  15.     [CommandMethod("RHB", CommandFlags.Modal)]
  16.     public void RHB()
  17.     {
  18.       Document doc = Application.DocumentManager.MdiActiveDocument;
  19.       Editor ed = doc.Editor;
  20.       PromptEntityOptions prOps = new PromptEntityOptions("\nSelect Hatch: ");
  21.       prOps.SetRejectMessage("\nNot a Hatch");
  22.       prOps.AddAllowedClass(typeof(Hatch), false);
  23.       PromptEntityResult prRes = ed.GetEntity(prOps);
  24.       if (prRes.Status != PromptStatus.OK) return;
  25.       using (Transaction tr = doc.TransactionManager.StartTransaction()) {
  26.         Hatch hatch = tr.GetObject(prRes.ObjectId, OpenMode.ForRead) as Hatch;
  27.         if (hatch != null) {
  28.           BlockTableRecord btr = tr.GetObject(hatch.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
  29.           if (btr == null) return;
  30.           Plane plane = hatch.GetPlane();
  31.           int nLoops = hatch.NumberOfLoops;
  32.           for (int i = 0; i < nLoops; i++) {
  33.             HatchLoop loop = hatch.GetLoopAt(i);
  34.             if (loop.IsPolyline) {
  35.               using (Polyline poly = new Polyline()) {
  36.                 int iVertex = 0;
  37.                 foreach (BulgeVertex bv in loop.Polyline) {
  38.                   poly.AddVertexAt(iVertex++, bv.Vertex, bv.Bulge, 0.0, 0.0);
  39.                 }
  40.                 btr.AppendEntity(poly);
  41.                 tr.AddNewlyCreatedDBObject(poly, true);
  42.               }
  43.             } else {
  44.               foreach (Curve2d cv in loop.Curves) {
  45.                 LineSegment2d line2d = cv as LineSegment2d;
  46.                 CircularArc2d arc2d = cv as CircularArc2d;
  47.                 EllipticalArc2d ellipse2d = cv as EllipticalArc2d;
  48.                 NurbCurve2d spline2d = cv as NurbCurve2d;
  49.                 if (line2d != null) {
  50.                   using (Line ent = new Line()) {
  51.                     ent.StartPoint = new Point3d(plane, line2d.StartPoint);
  52.                     ent.EndPoint = new Point3d(plane, line2d.EndPoint);
  53.                     btr.AppendEntity(ent);
  54.                     tr.AddNewlyCreatedDBObject(ent, true);
  55.                   }
  56.                 } else if (arc2d != null) {
  57.                   if (Math.Abs(arc2d.StartAngle - arc2d.EndAngle) < 1e-5) {
  58.                     using (Circle ent = new Circle(new Point3d(plane, arc2d.Center), plane.Normal, arc2d.Radius)) {
  59.                       btr.AppendEntity(ent);
  60.                       tr.AddNewlyCreatedDBObject(ent, true);
  61.                     }
  62.                   } else {
  63.                     double angle = new Vector3d(plane, arc2d.ReferenceVector).AngleOnPlane(plane);
  64.                     using (Arc ent = new Arc(new Point3d(plane, arc2d.Center), arc2d.Radius, arc2d.StartAngle + angle, arc2d.EndAngle + angle)) {
  65.                       btr.AppendEntity(ent);
  66.                       tr.AddNewlyCreatedDBObject(ent, true);
  67.                     }
  68.                   }
  69.                 } else if (ellipse2d != null) {
  70.                   //-------------------------------------------------------------------------------------------
  71.                   // Bug: Can not assign StartParam and EndParam of Ellipse:
  72.                   // Ellipse ent = new Ellipse(new Point3d(plane, e2d.Center), plane.Normal,
  73.                   //      new Vector3d(plane,e2d.MajorAxis) * e2d.MajorRadius,
  74.                   //      e2d.MinorRadius / e2d.MajorRadius, e2d.StartAngle, e2d.EndAngle);
  75.                   // ent.StartParam = e2d.StartAngle;
  76.                   // ent.EndParam = e2d.EndAngle;
  77.                   // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.StartParam' cannot be assigned to -- it is read only
  78.                   // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.EndParam' cannot be assigned to -- it is read only
  79.                   //---------------------------------------------------------------------------------------------
  80.                   // Workaround is using Reflection
  81.                   //
  82.                   using (Ellipse ent = new Ellipse(new Point3d(plane, ellipse2d.Center), plane.Normal,
  83.                        new Vector3d(plane, ellipse2d.MajorAxis) * ellipse2d.MajorRadius,
  84.                        ellipse2d.MinorRadius / ellipse2d.MajorRadius, ellipse2d.StartAngle, ellipse2d.EndAngle)) {
  85.                     ent.GetType().InvokeMember("StartParam", BindingFlags.SetProperty, null,
  86.                       ent, new object[] { ellipse2d.StartAngle });
  87.                     ent.GetType().InvokeMember("EndParam", BindingFlags.SetProperty, null,
  88.                       ent, new object[] { ellipse2d.EndAngle });
  89.                     btr.AppendEntity(ent);
  90.                     tr.AddNewlyCreatedDBObject(ent, true);
  91.                   }
  92.  
  93.                 } else if (spline2d != null) {
  94.                   if (spline2d.HasFitData) {
  95.                     NurbCurve2dFitData n2fd = spline2d.FitData;
  96.                     using (Point3dCollection p3ds = new Point3dCollection()) {
  97.                       foreach (Point2d p in n2fd.FitPoints) p3ds.Add(new Point3d(plane, p));
  98.                       using (Spline ent = new Spline(p3ds, new Vector3d(plane, n2fd.StartTangent), new Vector3d(plane, n2fd.EndTangent),
  99.                         /* n2fd.KnotParam, */  n2fd.Degree, n2fd.FitTolerance.EqualPoint)) {
  100.                         btr.AppendEntity(ent);
  101.                         tr.AddNewlyCreatedDBObject(ent, true);
  102.                       }
  103.                     }
  104.                   } else {
  105.                     NurbCurve2dData n2fd = spline2d.DefinitionData;
  106.                     using (Point3dCollection p3ds = new Point3dCollection()) {
  107.                       DoubleCollection knots = new DoubleCollection(n2fd.Knots.Count);
  108.                       foreach (Point2d p in n2fd.ControlPoints) p3ds.Add(new Point3d(plane, p));
  109.                       foreach (double k in n2fd.Knots) knots.Add(k);
  110.                       double period = 0;
  111.                       using (Spline ent = new Spline(n2fd.Degree, n2fd.Rational,
  112.                                spline2d.IsClosed(), spline2d.IsPeriodic(out period),
  113.                                p3ds, knots, n2fd.Weights, n2fd.Knots.Tolerance, n2fd.Knots.Tolerance)) {
  114.                         btr.AppendEntity(ent);
  115.                         tr.AddNewlyCreatedDBObject(ent, true);
  116.                       }
  117.                     }
  118.                   }
  119.                 }
  120.               }
  121.             }
  122.           }
  123.         }
  124.         tr.Commit();
  125.       }
  126.     }
  127.   }
  128. }

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 3 of 5 (428 Views)

Re: Code posting

03-19-2013 03:31 PM in reply to: Alexander.Rivilis
  1. using System;
  2. using System.Reflection;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.EditorInput;
  8.  
  9. [assembly: CommandClass(typeof(Rivilis.Purge))]
  10.  
  11. namespace Rivilis
  12. {
  13.   public class Purge
  14.   {
  15.     [CommandMethod("MyPurgeAll")]
  16.     static public void MyPurgeAll()
  17.     {
  18.       // For AutoCAD 2013+
  19.       object adoc = Application.DocumentManager.MdiActiveDocument.GetAcadDocument();
  20.       // For AutoCAD 2012-
  21.       // object adoc = Application.DocumentManager.MdiActiveDocument.AcadDocument;
  22.       if (adoc != null) {
  23.         adoc.GetType().InvokeMember("PurgeAll", BindingFlags.InvokeMethod, null, adoc, null);
  24.       }
  25.     }
  26.   }
  27. }

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 4 of 5 (424 Views)

Re: Code posting

03-19-2013 03:48 PM in reply to: Alexander.Rivilis
  1. using System;
  2. using System.Reflection;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.EditorInput;
  8.  
  9. [assembly: CommandClass(typeof(Rivilis.HatchUtils))]
  10.  
  11. namespace Rivilis
  12. {
  13.   public class HatchUtils
  14.   {
  15.     [CommandMethod("RHB", CommandFlags.Modal)]
  16.     public void RHB()
  17.     {
  18.       Document doc = Application.DocumentManager.MdiActiveDocument;
  19.       Editor ed = doc.Editor;
  20.       PromptEntityOptions prOps = new PromptEntityOptions("\nSelect Hatch: ");
  21.       prOps.SetRejectMessage("\nNot a Hatch");
  22.       prOps.AddAllowedClass(typeof(Hatch), false);
  23.       PromptEntityResult prRes = ed.GetEntity(prOps);
  24.       if (prRes.Status != PromptStatus.OK) return;
  25.       using (Transaction tr = doc.TransactionManager.StartTransaction()) {
  26.         Hatch hatch = tr.GetObject(prRes.ObjectId, OpenMode.ForRead) as Hatch;
  27.         if (hatch != null) {
  28.           BlockTableRecord btr = tr.GetObject(hatch.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
  29.           if (btr == null) return;
  30.           Plane plane = hatch.GetPlane();
  31.           int nLoops = hatch.NumberOfLoops;
  32.           for (int i = 0; i < nLoops; i++) {
  33.             HatchLoop loop = hatch.GetLoopAt(i);
  34.             if (loop.IsPolyline) {
  35.               using (Polyline poly = new Polyline()) {
  36.                 int iVertex = 0;
  37.                 foreach (BulgeVertex bv in loop.Polyline) {
  38.                   poly.AddVertexAt(iVertex++, bv.Vertex, bv.Bulge, 0.0, 0.0);
  39.                 }
  40.                 btr.AppendEntity(poly);
  41.                 tr.AddNewlyCreatedDBObject(poly, true);
  42.               }
  43.             } else {
  44.               foreach (Curve2d cv in loop.Curves) {
  45.                 LineSegment2d line2d = cv as LineSegment2d;
  46.                 CircularArc2d arc2d = cv as CircularArc2d;
  47.                 EllipticalArc2d ellipse2d = cv as EllipticalArc2d;
  48.                 NurbCurve2d spline2d = cv as NurbCurve2d;
  49.                 if (line2d != null) {
  50.                   using (Line ent = new Line()) {
  51.                     ent.StartPoint = new Point3d(plane, line2d.StartPoint);
  52.                     ent.EndPoint = new Point3d(plane, line2d.EndPoint);
  53.                     btr.AppendEntity(ent);
  54.                     tr.AddNewlyCreatedDBObject(ent, true);
  55.                   }
  56.                 } else if (arc2d != null) {
  57.                   if (Math.Abs(arc2d.StartAngle - arc2d.EndAngle) < 1e-5) {
  58.                     using (Circle ent = new Circle(new Point3d(plane, arc2d.Center), plane.Normal, arc2d.Radius)) {
  59.                       btr.AppendEntity(ent);
  60.                       tr.AddNewlyCreatedDBObject(ent, true);
  61.                     }
  62.                   } else {
  63.                     double angle = new Vector3d(plane, arc2d.ReferenceVector).AngleOnPlane(plane);
  64.                     using (Arc ent = new Arc(new Point3d(plane, arc2d.Center), arc2d.Radius, arc2d.StartAngle + angle, arc2d.EndAngle + angle)) {
  65.                       btr.AppendEntity(ent);
  66.                       tr.AddNewlyCreatedDBObject(ent, true);
  67.                     }
  68.                   }
  69.                 } else if (ellipse2d != null) {
  70.                   //-------------------------------------------------------------------------------------------
  71.                   // Bug: Can not assign StartParam and EndParam of Ellipse:
  72.                   // Ellipse ent = new Ellipse(new Point3d(plane, e2d.Center), plane.Normal,
  73.                   //      new Vector3d(plane,e2d.MajorAxis) * e2d.MajorRadius,
  74.                   //      e2d.MinorRadius / e2d.MajorRadius, e2d.StartAngle, e2d.EndAngle);
  75.                   // ent.StartParam = e2d.StartAngle;
  76.                   // ent.EndParam = e2d.EndAngle;
  77.                   // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.StartParam' cannot be assigned to -- it is read only
  78.                   // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.EndParam' cannot be assigned to -- it is read only
  79.                   //---------------------------------------------------------------------------------------------
  80.                   // Workaround is using Reflection
  81.                   //
  82.                   using (Ellipse ent = new Ellipse(new Point3d(plane, ellipse2d.Center), plane.Normal,
  83.                        new Vector3d(plane, ellipse2d.MajorAxis) * ellipse2d.MajorRadius,
  84.                        ellipse2d.MinorRadius / ellipse2d.MajorRadius, ellipse2d.StartAngle, ellipse2d.EndAngle)) {
  85.                     ent.GetType().InvokeMember("StartParam", BindingFlags.SetProperty, null,
  86.                       ent, new object[] { ellipse2d.StartAngle });
  87.                     ent.GetType().InvokeMember("EndParam", BindingFlags.SetProperty, null,
  88.                       ent, new object[] { ellipse2d.EndAngle });
  89.                     btr.AppendEntity(ent);
  90.                     tr.AddNewlyCreatedDBObject(ent, true);
  91.                   }
  92.  
  93.                 } else if (spline2d != null) {
  94.                   if (spline2d.HasFitData) {
  95.                     NurbCurve2dFitData n2fd = spline2d.FitData;
  96.                     using (Point3dCollection p3ds = new Point3dCollection()) {
  97.                       foreach (Point2d p in n2fd.FitPoints) p3ds.Add(new Point3d(plane, p));
  98.                       using (Spline ent = new Spline(p3ds, new Vector3d(plane, n2fd.StartTangent), new Vector3d(plane, n2fd.EndTangent),
  99.                         /* n2fd.KnotParam, */  n2fd.Degree, n2fd.FitTolerance.EqualPoint)) {
  100.                         btr.AppendEntity(ent);
  101.                         tr.AddNewlyCreatedDBObject(ent, true);
  102.                       }
  103.                     }
  104.                   } else {
  105.                     NurbCurve2dData n2fd = spline2d.DefinitionData;
  106.                     using (Point3dCollection p3ds = new Point3dCollection()) {
  107.                       DoubleCollection knots = new DoubleCollection(n2fd.Knots.Count);
  108.                       foreach (Point2d p in n2fd.ControlPoints) p3ds.Add(new Point3d(plane, p));
  109.                       foreach (double k in n2fd.Knots) knots.Add(k);
  110.                       double period = 0;
  111.                       using (Spline ent = new Spline(n2fd.Degree, n2fd.Rational,
  112.                                spline2d.IsClosed(), spline2d.IsPeriodic(out period),
  113.                                p3ds, knots, n2fd.Weights, n2fd.Knots.Tolerance, n2fd.Knots.Tolerance)) {
  114.                         btr.AppendEntity(ent);
  115.                         tr.AddNewlyCreatedDBObject(ent, true);
  116.                       }
  117.                     }
  118.                   }
  119.                 }
  120.               }
  121.             }
  122.           }
  123.         }
  124.         tr.Commit();
  125.       }
  126.     }
  127.   }
  128. }

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 5 of 5 (416 Views)

Re: Code posting

03-19-2013 04:05 PM in reply to: Alexander.Rivilis

using System;

using System.Reflection;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.EditorInput;

 

[assembly: CommandClass(typeof(Rivilis.HatchUtils))]

 

namespace Rivilis

{

  public class HatchUtils

  {

    [CommandMethod("RHB", CommandFlags.Modal)]

    public void RHB()

    {

      Document doc = Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

      PromptEntityOptions prOps = new PromptEntityOptions("\nSelect Hatch: ");

      prOps.SetRejectMessage("\nNot a Hatch");

      prOps.AddAllowedClass(typeof(Hatch), false);

      PromptEntityResult prRes = ed.GetEntity(prOps);

      if (prRes.Status != PromptStatus.OK) return;

      using (Transaction tr = doc.TransactionManager.StartTransaction()) {

        Hatch hatch = tr.GetObject(prRes.ObjectId, OpenMode.ForRead) as Hatch;

        if (hatch != null) {

          BlockTableRecord btr = tr.GetObject(hatch.OwnerId, OpenMode.ForWrite) as BlockTableRecord;

          if (btr == null) return;

          Plane plane = hatch.GetPlane();

          int nLoops = hatch.NumberOfLoops;

          for (int i = 0; i < nLoops; i++) {

            HatchLoop loop = hatch.GetLoopAt(i);

            if (loop.IsPolyline) {

              using (Polyline poly = new Polyline()) {

                int iVertex = 0;

                foreach (BulgeVertex bv in loop.Polyline) {

                  poly.AddVertexAt(iVertex++, bv.Vertex, bv.Bulge, 0.0, 0.0);

                }

                btr.AppendEntity(poly);

                tr.AddNewlyCreatedDBObject(poly, true);

              }

            } else {

              foreach (Curve2d cv in loop.Curves) {

                LineSegment2d line2d = cv as LineSegment2d;

                CircularArc2d arc2d = cv as CircularArc2d;

                EllipticalArc2d ellipse2d = cv as EllipticalArc2d;

                NurbCurve2d spline2d = cv as NurbCurve2d;

                if (line2d != null) {

                  using (Line ent = new Line()) {

                    ent.StartPoint = new Point3d(plane, line2d.StartPoint);

                    ent.EndPoint = new Point3d(plane, line2d.EndPoint);

                    btr.AppendEntity(ent);

                    tr.AddNewlyCreatedDBObject(ent, true);

                  }

                } else if (arc2d != null) {

                  if (Math.Abs(arc2d.StartAngle - arc2d.EndAngle) < 1e-5) {

                    using (Circle ent = new Circle(new Point3d(plane, arc2d.Center), plane.Normal, arc2d.Radius)) {

                      btr.AppendEntity(ent);

                      tr.AddNewlyCreatedDBObject(ent, true);

                    }

                  } else {

                    double angle = new Vector3d(plane,

arc2d.ReferenceVector).AngleOnPlane(plane);

                    using (Arc ent = new Arc(new Point3d(plane, arc2d.Center),

arc2d.Radius, arc2d.StartAngle + angle, arc2d.EndAngle + angle)) {

                      btr.AppendEntity(ent);

                      tr.AddNewlyCreatedDBObject(ent, true);

                    }

                  }

                } else if (ellipse2d != null) {

                  //-------------------------------------------------------------------------------------------

                  // Bug: Can not assign StartParam and EndParam of Ellipse:

                  // Ellipse ent = new Ellipse(new Point3d(plane, e2d.Center), plane.Normal,

                  //      new Vector3d(plane,e2d.MajorAxis) * e2d.MajorRadius,

                  //      e2d.MinorRadius / e2d.MajorRadius, e2d.StartAngle, e2d.EndAngle);

                  // ent.StartParam = e2d.StartAngle;

                  // ent.EndParam = e2d.EndAngle;

                  // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.StartParam' cannot be assigned to -- it is read only

                  // error CS0200: Property or indexer 'Autodesk.AutoCAD.DatabaseServices.Curve.EndParam' cannot be assigned to -- it is read only

                  //---------------------------------------------------------------------------------------------

                  // Workaround is using Reflection

                  //

                  using (Ellipse ent = new Ellipse(new Point3d(plane, ellipse2d.Center), plane.Normal,

                       new Vector3d(plane, ellipse2d.MajorAxis) * ellipse2d.MajorRadius,

                       ellipse2d.MinorRadius / ellipse2d.MajorRadius, ellipse2d.StartAngle, ellipse2d.EndAngle)) {

                    ent.GetType().InvokeMember("StartParam", BindingFlags.SetProperty, null,

                      ent, new object[] { ellipse2d.StartAngle });

                    ent.GetType().InvokeMember("EndParam", BindingFlags.SetProperty, null,

                      ent, new object[] { ellipse2d.EndAngle });

                    btr.AppendEntity(ent);

                    tr.AddNewlyCreatedDBObject(ent, true);

                  }

 

                } else if (spline2d != null) {

                  if (spline2d.HasFitData) {

                    NurbCurve2dFitData n2fd = spline2d.FitData;

                    using (Point3dCollection p3ds = new Point3dCollection()) {

                      foreach (Point2d p in n2fd.FitPoints) p3ds.Add(new Point3d(plane, p));

                      using (Spline ent = new Spline(p3ds, new Vector3d(plane, n2fd.StartTangent), new Vector3d(plane, n2fd.EndTangent),

                        /* n2fd.KnotParam, */  n2fd.Degree, n2fd.FitTolerance.EqualPoint)) {

                        btr.AppendEntity(ent);

                        tr.AddNewlyCreatedDBObject(ent, true);

                      }

                    }

                  } else {

                    NurbCurve2dData n2fd = spline2d.DefinitionData;

                    using (Point3dCollection p3ds = new Point3dCollection()) {

                      DoubleCollection knots = new DoubleCollection(n2fd.Knots.Count);

                      foreach (Point2d p in n2fd.ControlPoints) p3ds.Add(new Point3d(plane, p));

                      foreach (double k in n2fd.Knots) knots.Add(k);

                      double period = 0;

                      using (Spline ent = new Spline(n2fd.Degree, n2fd.Rational,

                               spline2d.IsClosed(), spline2d.IsPeriodic(out period),

                               p3ds, knots, n2fd.Weights, n2fd.Knots.Tolerance, n2fd.Knots.Tolerance)) {

                        btr.AppendEntity(ent);

                        tr.AddNewlyCreatedDBObject(ent, true);

                      }

                    }

                  }

                }

              }

            }

          }

        }

        tr.Commit();

      }

    }

  }

}


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!