Author Topic: Creating 3D Solids programmatically  (Read 857 times)

0 Members and 1 Guest are viewing this topic.

Offline Patriiick

  • Administrator
  • *****
  • Posts: 130
  • Karma: +1/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
  • Twitter: @Patriiick
Creating 3D Solids programmatically
« on: August 25, 2011, 02:54:39 PM »
Is it possible to create 3D solids programmatically like for example this one:

[attach=1]


target audience:{advanced}

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: Creating 3D Solids programmatically
« Reply #1 on: August 25, 2011, 08:49:19 PM »
Hi,

Look at the Autodesk.AutoCAD.DatabaseServices.Solid3d class in the ObjectARX doc.

Anyway, here's an example:
Code: [Select]
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace ExtrudedSolidSample
{
    public class Commands
    {
        [CommandMethod("Test")]
        public void Test()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Create a closed polyline (it have to be disposed because it won't be added to the database)
                using (Polyline pline = new Polyline(12))
                {
                    pline.AddVertexAt(0, new Point2d(100.0, 100.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(200.0, 100.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(200.0, 130.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(180.0, 130.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(180.0, 170.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(200.0, 170.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(200.0, 200.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(100.0, 200.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(100.0, 170.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(120.0, 170.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(120.0, 130.0), 0.0, 0.0, 0.0);
                    pline.AddVertexAt(0, new Point2d(100.0, 130.0), 0.0, 0.0, 0.0);
                    pline.Closed = true;
                    DBObjectCollection curves = new DBObjectCollection { pline };

                    // Create a region (it have to be disposed because it won't be added to the database)
                    using(DBObjectCollection regions = Region.CreateFromCurves(curves))
                    {
                        // Create a solid 3d (it will be added to the database)
                        Solid3d solid = new Solid3d();
                        solid.Extrude((Region)regions[0], 500.0, 0.0);
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr =
                            (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        btr.AppendEntity(solid);
                        tr.AddNewlyCreatedDBObject(solid, true);
                       
                        // Create a slicing plane
                        Plane plane = new Plane(new Point3d(200.0, 200.0, 500.0), new Vector3d(0.0, 1.0, -1.0));

                        // Slice the solid 3d with the plane
                        solid.Slice(plane, true);
                    }
                }
                tr.Commit();
            }
        }
    }
}