Author Topic: Create a 3d-Cylinder in .net?  (Read 4199 times)

0 Members and 1 Guest are viewing this topic.

MarioR

  • Newt
  • Posts: 64
Create a 3d-Cylinder in .net?
« on: February 19, 2013, 08:08:32 AM »
Hello,

i  have 2 Point3d (startPoint and endPoint) with radius and now i like create a cylinder
on dotNet.

version a)
i create a Autodesk.AutoCAD.Geometry.Cylinder  (from http://forums.autodesk.com/t5/NET/How-to-draw-a-cylinder-using-vb-net/m-p/1383998)
the Cylinder i cant append to blocktablerecord Cylinder isnt children of enity

version b)
i create a Autodesk.AutoCAD.DatabaseServices.Solid3d with Solid3d.CreateFrustum(...)
and transform by Martix3d from Vector start-end...

The  Result of it is not a Cylinder on AutoCAD-Properties.

A manual construct cylinder by _cylinder has on properties-tab a section "Geometry" with solid3d-Type=cylinder. But the "Solid3d.CreateFrustum" has not section "Geometrie".

Has any one an litle Example for create a Cylinder with startpoint, Vector and radius.
At least one example that result is an correct ly cylinder.

regards Mario

fixo

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #1 on: February 19, 2013, 08:49:44 AM »
Try this code, change circle radius and height of cylinder to suit:
Code: [Select]
      [CommandMethod("TestCreateExtrudedCylinder")]
        public void CreateExtrudedCylinder()
        {
            //Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("delobj", 1);

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            PromptSelectionOptions pso = new PromptSelectionOptions();

            pso.SingleOnly = true;

            pso.SinglePickInSpace = true;

            pso.MessageForAdding = "\nPlease select a circle: ";

            pso.MessageForRemoval = "Failed to select a circle";

            pso.PrepareOptionalDetails = true;

            SelectionFilter filter =
                new SelectionFilter(
                new TypedValue[] { new TypedValue(0, "circle")});

            //select the polyline
            PromptSelectionResult sres = ed.GetSelection(pso, filter);

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

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                //get the boundary curves of the polyline

                ObjectId objId = sres.Value[0].ObjectId;

                Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;

                Circle circ = ent as Circle;

                if (circ == null) return;

                DBObjectCollection regs = new DBObjectCollection();

                regs.Add(circ);

                // Create a region from the circle.

                DBObjectCollection regions = new DBObjectCollection();

                regions = Region.CreateFromCurves(regs);
               
                if (regions.Count == 0)
                {
                    ed.WriteMessage("\nFailed to create region\n");
                    return;
                }

                Region reg = (Region)regions[0];

                // Extrude the region to create a solid.

                Solid3d sol = new Solid3d();

                sol.RecordHistory = true;

                sol.ShowHistory = true;

                sol.Extrude(reg, 10.0, 0.0);// < --- set extruded height and taper angle here

                ObjectId solId = ObjectId.Null;

                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                solId = btr.AppendEntity(sol);

                tr.AddNewlyCreatedDBObject(sol, true);

                if (!circ.IsWriteEnabled) circ.UpgradeOpen();

                circ.Erase();

                tr.Commit();
            }

        }

You may want to use the code from this page too:
http://www.acadnetwork.com/topic-288.0.html
« Last Edit: February 19, 2013, 09:21:25 AM by fixo »

TheMaster

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #2 on: February 19, 2013, 11:19:57 AM »
Hello,

i  have 2 Point3d (startPoint and endPoint) with radius and now i like create a cylinder
on dotNet.

version a)
i create a Autodesk.AutoCAD.Geometry.Cylinder  (from http://forums.autodesk.com/t5/NET/How-to-draw-a-cylinder-using-vb-net/m-p/1383998)
the Cylinder i cant append to blocktablerecord Cylinder isnt children of enity

version b)
i create a Autodesk.AutoCAD.DatabaseServices.Solid3d with Solid3d.CreateFrustum(...)
and transform by Martix3d from Vector start-end...

The  Result of it is not a Cylinder on AutoCAD-Properties.

A manual construct cylinder by _cylinder has on properties-tab a section "Geometry" with solid3d-Type=cylinder. But the "Solid3d.CreateFrustum" has not section "Geometrie".

Has any one an litle Example for create a Cylinder with startpoint, Vector and radius.
At least one example that result is an correct ly cylinder.

regards Mario

No need to waste time with Fixo's code, as he doesn't understand your question.

Solid primitives like Cylinders can only be created via the command line. The APIs for them are not exposed and are not easily usable, so the only practicle solution is to use command line to do it.

You can use the  wrapper for the Editor's undocumented RunCommand method that's been posted on this site (sorry don't have a link handy), to run the CYLINDER command from your code.


fixo

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #3 on: February 19, 2013, 02:29:50 PM »
Sorry Mario, TT is right.
Well another more attempt, I can't reach at RunCommand,
still working in A2010 only, better test it in 3d view
 
Code: [Select]
       [CommandMethod("testCylinderByVector")]
        public void DrawExtrudedCylinderByVector()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            Point3d cp,vecpt;

            PromptPointOptions ptopts = new PromptPointOptions("\nSelect center point of circel: ");

            PromptPointResult ptres = ed.GetPoint(ptopts);

            // Exit if the user presses ESC or cancels the command
            if (ptres.Status != PromptStatus.OK) return;
            // Center point
            cp = ptres.Value;
            ptopts.Message = "\nSpecify direction vector: ";
            ptopts.BasePoint = cp;
            ptopts.UseBasePoint = true;
            ptres = ed.GetPoint(ptopts);
            // Exit if the user presses ESC or cancels the command
            if (ptres.Status != PromptStatus.OK) return;
            // Center point
            vecpt = ptres.Value;
            Vector3d vec = (vecpt - cp).GetNormal();

            PromptDistanceOptions pdo = new PromptDistanceOptions("\nEnter a radius: ");
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = 100.0;
            pdo.BasePoint = cp;
            pdo.UseBasePoint = true;
            pdo.UseDashedLine = true;

            PromptDoubleResult res;
            res = ed.GetDistance(pdo);
            if (res.Status != PromptStatus.OK)
                return;

            double rad = res.Value;

            pdo = new PromptDistanceOptions("\nEnter cylinder height: ");
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = 1000.0;
            pdo.BasePoint = cp;
            pdo.UseBasePoint = true;
            pdo.UseDashedLine = true;

            res = ed.GetDistance(pdo);
            if (res.Status != PromptStatus.OK)
                return;

            double height = res.Value;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                Circle circ = new Circle(cp, Vector3d.ZAxis, rad);

                circ.Normal = vec;

                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                ObjectId objId = btr.AppendEntity(circ);

                tr.AddNewlyCreatedDBObject(circ, true);

                // Get the boundary curves to create a region
                DBObjectCollection regs = new DBObjectCollection();

                regs.Add(circ);

                // Create a region from the circle.

                DBObjectCollection regions = new DBObjectCollection();

                regions = Region.CreateFromCurves(regs);

                if (regions.Count == 0)
                {
                    ed.WriteMessage("\nFailed to create region\n");
                    return;
                }

                Region reg = (Region)regions[0];

                // Extrude the region to create a solid.

                Solid3d sol = new Solid3d();

                sol.RecordHistory = true;// important

                sol.ShowHistory = true;// important

                sol.Extrude(reg, height, 0.0);// < --- set taper angle here

                ObjectId solId = ObjectId.Null;

                solId = btr.AppendEntity(sol);

                tr.AddNewlyCreatedDBObject(sol, true);

                if (!circ.IsWriteEnabled) circ.UpgradeOpen();

                circ.Erase();

                tr.Commit();
            }

        }

MarioR

  • Newt
  • Posts: 64
Re: Create a 3d-Cylinder in .net?
« Reply #4 on: February 20, 2013, 12:19:41 AM »
Hello Fixo,

thanks, you're my hero!  :kewl:

with best regards
Mario

fixo

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #5 on: February 20, 2013, 12:51:37 AM »
Glad to help
Kind regards,
Oleg

TheMaster

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #6 on: February 20, 2013, 01:00:00 AM »
Sorry Mario, TT is right.
Well another more attempt, I can't reach at RunCommand,
still working in A2010 only, better test it in 3d view
 
Code: [Select]
       [CommandMethod("testCylinderByVector")]
        public void DrawExtrudedCylinderByVector()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            Point3d cp,vecpt;

            PromptPointOptions ptopts = new PromptPointOptions("\nSelect center point of circel: ");

            PromptPointResult ptres = ed.GetPoint(ptopts);

            // Exit if the user presses ESC or cancels the command
            if (ptres.Status != PromptStatus.OK) return;
            // Center point
            cp = ptres.Value;
            ptopts.Message = "\nSpecify direction vector: ";
            ptopts.BasePoint = cp;
            ptopts.UseBasePoint = true;
            ptres = ed.GetPoint(ptopts);
            // Exit if the user presses ESC or cancels the command
            if (ptres.Status != PromptStatus.OK) return;
            // Center point
            vecpt = ptres.Value;
            Vector3d vec = (vecpt - cp).GetNormal();

            PromptDistanceOptions pdo = new PromptDistanceOptions("\nEnter a radius: ");
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = 100.0;
            pdo.BasePoint = cp;
            pdo.UseBasePoint = true;
            pdo.UseDashedLine = true;

            PromptDoubleResult res;
            res = ed.GetDistance(pdo);
            if (res.Status != PromptStatus.OK)
                return;

            double rad = res.Value;

            pdo = new PromptDistanceOptions("\nEnter cylinder height: ");
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = 1000.0;
            pdo.BasePoint = cp;
            pdo.UseBasePoint = true;
            pdo.UseDashedLine = true;

            res = ed.GetDistance(pdo);
            if (res.Status != PromptStatus.OK)
                return;

            double height = res.Value;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                Circle circ = new Circle(cp, Vector3d.ZAxis, rad);

                circ.Normal = vec;

                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                ObjectId objId = btr.AppendEntity(circ);

                tr.AddNewlyCreatedDBObject(circ, true);

                // Get the boundary curves to create a region
                DBObjectCollection regs = new DBObjectCollection();

                regs.Add(circ);

                // Create a region from the circle.

                DBObjectCollection regions = new DBObjectCollection();

                regions = Region.CreateFromCurves(regs);

                if (regions.Count == 0)
                {
                    ed.WriteMessage("\nFailed to create region\n");
                    return;
                }

                Region reg = (Region)regions[0];

                // Extrude the region to create a solid.

                Solid3d sol = new Solid3d();

                sol.RecordHistory = true;// important

                sol.ShowHistory = true;// important

                sol.Extrude(reg, height, 0.0);// < --- set taper angle here

                ObjectId solId = ObjectId.Null;

                solId = btr.AppendEntity(sol);

                tr.AddNewlyCreatedDBObject(sol, true);

                if (!circ.IsWriteEnabled) circ.UpgradeOpen();

                circ.Erase();

                tr.Commit();
            }

        }

Fixo, run your code and then select the resulting object and look at the Properties Palette.

In "Solid Type" property, what does it say?  Does it say "Cylinder" or does it say "Extrusion" ???

Do you see a "Radius" property ?

Try the same on the result of using the Cylinder command, and what differences do you see in the Properties Palette?

fixo

  • Guest
Re: Create a 3d-Cylinder in .net?
« Reply #7 on: February 20, 2013, 02:21:53 AM »
I knew that, thanks