.NET

Reply
Member
schaudha
Posts: 4
Registered: ‎03-27-2013
Message 1 of 9 (315 Views)
Accepted Solution

Finding a sphere in an AutoCAD database

315 Views, 8 Replies
03-27-2013 01:30 AM

Hello,

 

As part of the automation requirements we are working on (through the .NET interface), we would like to find out whether a given drawing contains a sphere and if it does we would like to know its properties. We are doing something like this, to loop through the database and am able to get all Solid3d type of objects.

 

What I would like to know, if a given solid3d object is a sphere or otherwise. Can someone please help me with the same.

 

The code snippet is pasted here for reference:

                   

                   

                   

bool bContainsSphere = false;

                   

foreach (ObjectId Id in Ids)

{

                       

  DBObject oObject = ts.GetObject(Id, OpenMode.ForRead);                  

  if (oObject.GetType().Name == "Solid3d")

     {

                           

    Solid3d obj = oObject asSolid3d;

                      

     // How do I test if the Solid3d instance is a sphere?

                           

     }

                   

  if (bContainsSphere == false)

     {

        // Do something ....

     }

}

 

Use school math to find sphere,

give this a try

 [CommandMethod("ahha")]
public static void testSelectFunction()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
Point3d center=new Point3d(); double vol=0; double rad=0;
foreach (ObjectId id in btr)
{
if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Solid3d))))
{
Entity obj = (Entity)tr.GetObject(id, OpenMode.ForRead);
Solid3d sol = obj as Solid3d;
if (sol != null)
{
center = sol.MassProperties.Centroid;
vol = sol.MassProperties.Volume;
Extents3d ext = sol.MassProperties.Extents;
rad = (ext.MaxPoint.X - ext.MinPoint.X) / 2;
if ((ext.MaxPoint.X - ext.MinPoint.X) - (ext.MaxPoint.Y - ext.MinPoint.Y) < 1e-10 && (vol - 4.0 / Math.PI * Math.Pow(rad, 3) * 3.0) < 1e-10)
{
ed.WriteMessage("\nSphere found at {0:f3}\nRadius: {1:f3}\nVolum: {2:f5}", center,rad,vol);
}
}
}
}
}
}

 

Hi,

 

Take care with the SolidType COM property, it throws an exception if the 3d solid is not a primitive (box, sphere, cone, ...) or a simple solid created with a single command (extrusion, revolution, sweep, loft).

Curiously, the SolidType string is returned in the culture langage of the autoCAD version ("Boîte", "Sphère", "Cône", "Cylindre" in French).

 

Here's the snippet I wrote while Tony was replying:

 [CommandMethod("Test")]
public void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a 3d solid: ");
peo.SetRejectMessage("Only a 3d solid !");
peo.AddAllowedClass(typeof(Solid3d), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Solid3d solid = (Solid3d)tr.GetObject(per.ObjectId, OpenMode.ForRead);
dynamic acadSolid = solid.AcadObject;
try
{
ed.WriteMessage("\n{0}", acadSolid.SolidType);
}
catch
{
ed.WriteMessage("\nUnknown solid type");
}
tr.Commit();
}
}

 

Moderator
Alexander.Rivilis
Posts: 1,431
Registered: ‎04-09-2008
Message 2 of 9 (303 Views)

Re: Finding a sphere in an AutoCAD database

03-27-2013 02:36 AM in reply to: schaudha

Please read this topic carefully: http://forums.autodesk.com/t5/NET/Solid3d-properties/m-p/3792994


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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 9 (283 Views)

Re: Finding a sphere in an AutoCAD database

03-27-2013 09:22 AM in reply to: schaudha

Use school math to find sphere,

give this a try

        [CommandMethod("ahha")]
        public static void testSelectFunction()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
 
            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
                Point3d center=new Point3d(); double vol=0; double rad=0;
                foreach (ObjectId id in btr)
                {
                    if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Solid3d))))
                    {
                        Entity obj = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        Solid3d sol = obj as Solid3d;
                        if (sol != null)
                        {
                             center = sol.MassProperties.Centroid;
                             vol = sol.MassProperties.Volume;
                            Extents3d ext = sol.MassProperties.Extents;
                             rad = (ext.MaxPoint.X - ext.MinPoint.X) / 2;
                             if ((ext.MaxPoint.X - ext.MinPoint.X) - (ext.MaxPoint.Y - ext.MinPoint.Y) < 1e-10 && (vol - 4.0 / Math.PI * Math.Pow(rad, 3) * 3.0) < 1e-10)
                            {
                                
                                ed.WriteMessage("\nSphere found at {0:f3}\nRadius: {1:f3}\nVolum: {2:f5}", center,rad,vol);
 
                            }
                        }
                    }
                }
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Member
schaudha
Posts: 4
Registered: ‎03-27-2013
Message 4 of 9 (268 Views)

Re: Finding a sphere in an AutoCAD database

03-27-2013 10:10 PM in reply to: Hallex

Thanks Hallex. This works very well.

In the future, I can also use the extents to process other Solid3d objects in the database.

 

Regards,

SC

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 9 (238 Views)

Re: Finding a sphere in an AutoCAD database

03-28-2013 05:32 AM in reply to: schaudha

If you created the Spheres using the AutoCAD SPHERE command, you can just check the SolidType property of the AutoCAD COM wrapper:

 

     dynamic acadObj = solid3dObject.AcadObject;

     string solidType = acadObj.SolidType;

     if( solidType == "Sphere" )

        Console.WriteLIne( "Found a sphere");

 

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 6 of 9 (227 Views)

Re: Finding a sphere in an AutoCAD database

03-28-2013 06:01 AM in reply to: DiningPhilosopher

Hi,

 

Take care with the SolidType COM property, it throws an exception if the 3d solid is not a primitive (box, sphere, cone, ...) or a simple solid created with a single command (extrusion, revolution, sweep, loft).

Curiously, the SolidType string is returned in the culture langage of the autoCAD version ("Boîte", "Sphère", "Cône", "Cylindre" in French).

 

Here's the snippet I wrote while Tony was replying:

        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a 3d solid: ");
            peo.SetRejectMessage("Only a 3d solid !");
            peo.AddAllowedClass(typeof(Solid3d), false);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Solid3d solid = (Solid3d)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                dynamic acadSolid = solid.AcadObject;
                try
                {
                    ed.WriteMessage("\n{0}", acadSolid.SolidType);
                }
                catch
                {
                    ed.WriteMessage("\nUnknown solid type");
                }
                tr.Commit();
            }
        }

 

Gilles Chanteau
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 9 (215 Views)

Re: Finding a sphere in an AutoCAD database

03-28-2013 07:09 AM in reply to: _gile

Hi Gilles, can you confirm that:

You create box using command "box", then

apply different material on any side of them,

then execute your routine, on my machine

I have return message : "Unknown solid type"

 

Kind regards,

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 8 of 9 (207 Views)

Re: Finding a sphere in an AutoCAD database

03-28-2013 07:30 AM in reply to: Hallex

No Oleg,

I can't reproduce what you decribe. The box is still a box ("Boîte" here).

However, just slice it and it becomes "unknown".

Gilles Chanteau
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 9 (200 Views)

Re: Finding a sphere in an AutoCAD database

03-28-2013 08:08 AM in reply to: _gile

Oh, sorry my bad

It's the same you talking about :manembarrassed:

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.