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 ....
}
}
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Solved by Hallex. Go to Solution.
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
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); } } } } } }
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
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");
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(); } }
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
Oh, sorry my bad
It's the same you talking about
Can't find what you're looking for? Ask the community or share your knowledge.