.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Finding a sphere in an AutoCAD database

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
schaudha
744 Views, 8 Replies

Finding a sphere in an AutoCAD database

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 ....

     }

}

 

8 REPLIES 8
Message 2 of 9

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

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 9
Hallex
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
Message 4 of 9
schaudha
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

 

Message 5 of 9

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");

 

Message 6 of 9
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9
Hallex
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
Message 8 of 9
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 9
Hallex
in reply to: _gile

Oh, sorry my bad

It's the same you talking about Man Embarassed

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost