Author Topic: Create object  (Read 4149 times)

0 Members and 1 Guest are viewing this topic.

feesa

  • Guest
Create object
« on: February 20, 2017, 02:06:51 AM »
Hi,

I am a .net Beginner,
I would be highly appericiated if any one Provide me a coding,
I need to creating a object from Geometric center of another object,
we have to ask user to select window, then from that window, I need to create polyline with angle given by the user,
I have attached a dwg for above description.

Thanks & Regards

feesa

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #1 on: February 20, 2017, 02:43:49 AM »
Hi,

Assuming you're a ".net Beginner", you shouldn't start with such a complex task. your goal requires an intermediate AutoCAD .net programming level with 3D geometry.
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #2 on: February 20, 2017, 08:47:35 AM »
Yes, I agree,
 i have made long jump,
Could you pls provide me coding for my query..

I have been planning to join some autocad  recognize institute to learn the basics,
If you could also provide me any ideas, links, materials to learn the .net
for autocad would be helpful...

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #3 on: February 20, 2017, 10:03:55 AM »
Hi,

You should first learn the .NET basics and a supportd language (i'd recommend C# which is the most used) outside of AutoCAD, you will fin links in the .NET Getting Started topic.
Then, you can find the official documentation and tutorials for AutoCAD .NET on this page.

Here's a quick and dirty example of what you attempt to do:
Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using System;
  7.  
  8. namespace Windows
  9. {
  10.     public class Commands
  11.     {
  12.         [CommandMethod("Test")]
  13.         public void Test()
  14.         {
  15.             Document doc = Application.DocumentManager.MdiActiveDocument;
  16.             Database db = doc.Database;
  17.             Editor ed = doc.Editor;
  18.  
  19.             // prompt for the window polyline
  20.             var entOpts = new PromptEntityOptions("\nSelect a window: ");
  21.             entOpts.SetRejectMessage("\nSelected object must be apolyline.");
  22.             entOpts.AddAllowedClass(typeof(Polyline), true);
  23.             var entRes = ed.GetEntity(entOpts);
  24.             if (entRes.Status != PromptStatus.OK)
  25.                 return;
  26.  
  27.             using (Transaction tr = db.TransactionManager.StartTransaction())
  28.             {
  29.                 var window = (Polyline)tr.GetObject(entRes.ObjectId, OpenMode.ForRead);
  30.  
  31.                 // get the window center
  32.                 Extents3d extents = window.GeometricExtents;
  33.                 Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  34.  
  35.                 // get the normal of the window plane
  36.                 Point2d pt = new Point2d(center.X, center.Y);
  37.                 Vector3d normal = window.Normal;
  38.  
  39.                 // check if the window is vertical
  40.                 if (!normal.IsPerpendicularTo(Vector3d.ZAxis))
  41.                 {
  42.                     ed.WriteMessage("\nThe window is not vertical.");
  43.                     return;
  44.                 }
  45.  
  46.                 // get the nnormal angle about XY plane
  47.                 var angle0 = normal.AngleOnPlane(new Plane());
  48.  
  49.                 // prompt for the first angle
  50.                 var angRes = ed.GetAngle("\nAngle 1: ");
  51.                 if (angRes.Status != PromptStatus.OK)
  52.                     return;
  53.                 var angle1 = angRes.Value;
  54.  
  55.                 // prompt for the second angle
  56.                 angRes = ed.GetAngle("\nAngle 2: ");
  57.                 if (angRes.Status != PromptStatus.OK)
  58.                     return;
  59.                 var angle2 = angRes.Value;
  60.  
  61.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  62.  
  63.                 // create the polyline
  64.                 using (var pline = new Polyline(3))
  65.                 {
  66.                     // add vertices
  67.                     pline.AddVertexAt(0, pt, 0.0, 0.0, 0.0);
  68.                     pline.AddVertexAt(1, Polar(pt, angle0 - angle1, 5000.0), 0.0, 0.0, 0.0);
  69.                     pline.AddVertexAt(2, Polar(pt, angle0 + angle1, 5000.0), 0.0, 0.0, 0.0);
  70.                     pline.Closed = true;
  71.  
  72.                     // transform the polyline
  73.                     pline.Elevation = center.Z;
  74.                     pline.TransformBy(Matrix3d.Rotation(angle2, normal.CrossProduct(Vector3d.ZAxis), center));
  75.  
  76.                     // add the polyline to current space block table record
  77.                     curSpace.AppendEntity(pline);
  78.                     tr.AddNewlyCreatedDBObject(pline, true);
  79.                 }
  80.                 tr.Commit();
  81.             }
  82.         }
  83.  
  84.         public Point2d Polar(Point2d basePoint, double angle, double distance)
  85.         {
  86.             return new Point2d(
  87.                 basePoint.X + distance * Math.Cos(angle),
  88.                 basePoint.Y + distance * Math.Sin(angle));
  89.         }
  90.     }
  91. }
  92.  
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #4 on: February 21, 2017, 07:16:12 AM »
Hi,
Many thanks
That's great,
It's working fine,

I need one more help in coding please,
I want to extrude that polyline(which is created from centre of window) in z axis for 5000mm

Could you pls provide me a code..


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #5 on: February 21, 2017, 07:58:35 AM »
Hi,

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using System;
  7.  
  8. namespace Windows
  9. {
  10.     public class Commands
  11.     {
  12.         [CommandMethod("Test")]
  13.         public void Test()
  14.         {
  15.             Document doc = Application.DocumentManager.MdiActiveDocument;
  16.             Database db = doc.Database;
  17.             Editor ed = doc.Editor;
  18.  
  19.             // prompt for the window polyline
  20.             var entOpts = new PromptEntityOptions("\nSelect a window: ");
  21.             entOpts.SetRejectMessage("\nSelected object must be apolyline.");
  22.             entOpts.AddAllowedClass(typeof(Polyline), true);
  23.             var entRes = ed.GetEntity(entOpts);
  24.             if (entRes.Status != PromptStatus.OK)
  25.                 return;
  26.  
  27.             using (Transaction tr = db.TransactionManager.StartTransaction())
  28.             {
  29.                 var window = (Polyline)tr.GetObject(entRes.ObjectId, OpenMode.ForRead);
  30.  
  31.                 // get the window center
  32.                 Extents3d extents = window.GeometricExtents;
  33.                 Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  34.  
  35.                 // get the normal of the window plane
  36.                 Point2d pt = new Point2d(center.X, center.Y);
  37.                 Vector3d normal = window.Normal;
  38.  
  39.                 // check if the window is vertical
  40.                 if (!normal.IsPerpendicularTo(Vector3d.ZAxis))
  41.                 {
  42.                     ed.WriteMessage("\nThe window is not vertical.");
  43.                     return;
  44.                 }
  45.  
  46.                 // get the nnormal angle about XY plane
  47.                 var angle0 = normal.AngleOnPlane(new Plane());
  48.  
  49.                 // prompt for the first angle
  50.                 var angRes = ed.GetAngle("\nAngle 1: ");
  51.                 if (angRes.Status != PromptStatus.OK)
  52.                     return;
  53.                 var angle1 = angRes.Value;
  54.  
  55.                 // prompt for the second angle
  56.                 angRes = ed.GetAngle("\nAngle 2: ");
  57.                 if (angRes.Status != PromptStatus.OK)
  58.                     return;
  59.                 var angle2 = angRes.Value;
  60.  
  61.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  62.  
  63.                 // create the polyline
  64.                 using (var pline = new Polyline(3))
  65.                 {
  66.                     // add vertices
  67.                     pline.AddVertexAt(0, pt, 0.0, 0.0, 0.0);
  68.                     pline.AddVertexAt(1, Polar(pt, angle0 - angle1, 5000.0), 0.0, 0.0, 0.0);
  69.                     pline.AddVertexAt(2, Polar(pt, angle0 + angle1, 5000.0), 0.0, 0.0, 0.0);
  70.                     pline.Closed = true;
  71.  
  72.                     // transform the polyline
  73.                     pline.Elevation = center.Z;
  74.                     pline.TransformBy(Matrix3d.Rotation(angle2, normal.CrossProduct(Vector3d.ZAxis), center));
  75.  
  76.                     // add the polyline to current space block table record
  77.                     curSpace.AppendEntity(pline);
  78.                     tr.AddNewlyCreatedDBObject(pline, true);
  79.                 }
  80.                 tr.Commit();
  81.             }
  82.         }
  83.  
  84.         [CommandMethod("CMD")]
  85.         public void Cmd()
  86.         {
  87.             Document doc = Application.DocumentManager.MdiActiveDocument;
  88.             Database db = doc.Database;
  89.             Editor ed = doc.Editor;
  90.  
  91.             // prompt for the window polyline
  92.             var entOpts = new PromptEntityOptions("\nSelect a window: ");
  93.             entOpts.SetRejectMessage("\nSelected object must be apolyline.");
  94.             entOpts.AddAllowedClass(typeof(Polyline), true);
  95.             var entRes = ed.GetEntity(entOpts);
  96.             if (entRes.Status != PromptStatus.OK)
  97.                 return;
  98.  
  99.             using (Transaction tr = db.TransactionManager.StartTransaction())
  100.             {
  101.                 var window = (Polyline)tr.GetObject(entRes.ObjectId, OpenMode.ForRead);
  102.  
  103.                 // get the window center
  104.                 Extents3d extents = window.GeometricExtents;
  105.                 Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  106.  
  107.                 // get the normal of the window plane
  108.                 Point2d pt = new Point2d(center.X, center.Y);
  109.                 Vector3d normal = window.Normal;
  110.  
  111.                 // check if the window is vertical
  112.                 if (!normal.IsPerpendicularTo(Vector3d.ZAxis))
  113.                 {
  114.                     ed.WriteMessage("\nThe window is not vertical.");
  115.                     return;
  116.                 }
  117.  
  118.                 // get the nnormal angle about XY plane
  119.                 var angle0 = normal.AngleOnPlane(new Plane());
  120.  
  121.                 // prompt for the first angle
  122.                 var angRes = ed.GetAngle("\nAngle 1: ");
  123.                 if (angRes.Status != PromptStatus.OK)
  124.                     return;
  125.                 var angle1 = angRes.Value;
  126.  
  127.                 // prompt for the second angle
  128.                 angRes = ed.GetAngle("\nAngle 2: ");
  129.                 if (angRes.Status != PromptStatus.OK)
  130.                     return;
  131.                 var angle2 = angRes.Value;
  132.  
  133.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  134.  
  135.                 // create the polyline
  136.                 using (var pline = new Polyline(3))
  137.                 {
  138.                     // add vertices
  139.                     pline.AddVertexAt(0, pt, 0.0, 0.0, 0.0);
  140.                     pline.AddVertexAt(1, Polar(pt, angle0 - angle1, 5000.0), 0.0, 0.0, 0.0);
  141.                     pline.AddVertexAt(2, Polar(pt, angle0 + angle1, 5000.0), 0.0, 0.0, 0.0);
  142.                     pline.Closed = true;
  143.  
  144.                     // transform the polyline
  145.                     pline.Elevation = center.Z;
  146.                     pline.TransformBy(Matrix3d.Rotation(angle2, normal.CrossProduct(Vector3d.ZAxis), center));
  147.  
  148.                     // project the polyline to XY plane
  149.                     var plane = new Plane();
  150.                     using (var flattenPline = new Polyline(3))
  151.                     {
  152.                         flattenPline.AddVertexAt(0, pline.GetPoint3dAt(0).Convert2d(plane), 0.0, 0.0, 0.0);
  153.                         flattenPline.AddVertexAt(1, pline.GetPoint3dAt(1).Convert2d(plane), 0.0, 0.0, 0.0);
  154.                         flattenPline.AddVertexAt(2, pline.GetPoint3dAt(2).Convert2d(plane), 0.0, 0.0, 0.0);
  155.                         flattenPline.Closed = true;
  156.                         flattenPline.Elevation = center.Z;
  157.                         var curves = new DBObjectCollection();
  158.  
  159.                         // create a region form the pojected polyline
  160.                         curves.Add(flattenPline);
  161.                         using (var regions = Region.CreateFromCurves(curves))
  162.                         {
  163.                             var region = (Region)regions[0];
  164.  
  165.                             // create and extruded solid from region
  166.                             using (var solid = new Solid3d())
  167.                             {
  168.                                 solid.Extrude(region, 5000.0, 0.0);
  169.                                 // slice the solid with the polyline plane
  170.                                 solid.Slice(pline.GetPlane());
  171.                                 // add the solid to current space block table record
  172.                                 curSpace.AppendEntity(solid);
  173.                                 tr.AddNewlyCreatedDBObject(solid, true);
  174.                             }
  175.                         }
  176.                     }
  177.  
  178.                     // add the polyline to current space block table record
  179.                     curSpace.AppendEntity(pline);
  180.                     tr.AddNewlyCreatedDBObject(pline, true);
  181.                 }
  182.                 tr.Commit();
  183.             }
  184.         }
  185.  
  186.         public Point2d Polar(Point2d basePoint, double angle, double distance)
  187.         {
  188.             return new Point2d(
  189.                 basePoint.X + distance * Math.Cos(angle),
  190.                 basePoint.Y + distance * Math.Sin(angle));
  191.         }
  192.     }
  193. }
  194.  
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #6 on: February 21, 2017, 08:48:07 AM »
it works..
Highly thankful for your Help..


feesa

  • Guest
Re: Create object
« Reply #7 on: March 09, 2017, 08:31:29 AM »
Hi,

I have tried the above,
but some time I got the result reverse.
I have attached the dwg.

can you tell pls.
« Last Edit: March 09, 2017, 08:39:34 AM by feesa »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #8 on: March 09, 2017, 02:00:11 PM »
Just revere the polyline ("window")
Right click > Polyline > Reverse
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #9 on: March 09, 2017, 10:02:34 PM »
Yes, many thanks
But in coding how could I know that I need to reverse this window?
When user selects the window at that time itself, coding should check weather the window needs to be Reverse.
Thanks & Regards

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #10 on: March 10, 2017, 01:46:06 AM »
As the program only knows a polyline somewhere in space, it cannot know if the polyline Normal is in the "good" direction...
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #11 on: March 10, 2017, 02:22:01 AM »
One more thing pls,
In coding where it's considering the direction as good?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #12 on: March 10, 2017, 02:36:42 AM »
Only considering the polyline, the code cannot know anything at all about what's the "good direction".
To evaluate the "good direction", it also needs to know the solid position about the polyline Normal.

Your request is not trivial at all and it looks like you want sombody to do your work for you...
Speaking English as a French Frog

feesa

  • Guest
Re: Create object
« Reply #13 on: March 10, 2017, 03:53:04 AM »
Many Thanks for your replays,

I didn't really mean like that,
Since I was in Learning stage, I was just finding the guidance....


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create object
« Reply #14 on: March 10, 2017, 04:15:07 AM »
So, if you get the solid by coding, you can compare the distance from the solid.MassProperties.Centroid to the 'center' of the window with the distance from the solid centroid to the 'center' plus the 'normal' (center + normal).
If the second one is less than the first one, you have to negate the normal (normal = normal.Negate();).
Speaking English as a French Frog