Author Topic: how to select hatches and DBText and get the center points of the hatches  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Hi, everyone.
I got a drawing, with a hatch presents a site, and a DBText present the site's name, seen as the image. There are only two kinds of hatchs: a single triangle, the other is a circle with a triangle inside. Other entities have already been removed by me by hand.
After selecting the hatch, some points are showing, so I believe I can get the center point's coordinate as the site's coordinate.

Now, I need to read all the hatches and DBTexts. Then comes the question: how can I get the center point of the hatch?
Here is all I got at the present:
Code: [Select]

//Select hatch and DBText together
 public bool Select(List<Point3d> hatchPointLst,List<DBText> nameLst)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
           
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {                   
                    SelectionFilter flt = new SelectionFilter(new TypedValue[] { new TypedValue(0, "TEXT,HATCH") });  // hatch and DBText

                    PromptSelectionOptions optSel = new PromptSelectionOptions();
                    optSel.MessageForAdding = "\nDraw the cursor to select:";
                    PromptSelectionResult resSel = ed.GetSelection(optSel, flt);

                    if (resSel.Status != PromptStatus.OK)
                        return false;

                    SelectionSet selSet = resSel.Value;
                    ObjectId[] ids = selSet.GetObjectIds();                   
                 
                    foreach (ObjectId sSetEntId in ids)
                    {
                        Entity ent = (Entity)trans.GetObject(sSetEntId, OpenMode.ForRead);
                        if (ent.GetType().Name == "Hatch")
                        {
                            Hatch myHatch = (Hatch) trans.GetObject(sSetEntId, OpenMode.ForRead);
                            [color=red]//Now, how to get the center point of the hatch.[/color]
                            //Then the center point can be put into the PointList

                        }       //End of the if (ent.GetType().Name == "Hatch")
                        else if (ent.GetType().Name == "DBText")
                        {
                            DBText myText = (DBText)trans.GetObject(sSetEntId, OpenMode.ForRead);
                            //Collect the DBText
                            nameLst.Add(myText);
                           }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                    return false;
                }

                trans.Commit();

                //
                return true;
            }
        }
« Last Edit: May 20, 2014, 08:18:52 AM by 闻仲 »

Gasty

  • Newt
  • Posts: 90
Hi,

Did you check the docs on the hatch object/class? In order to get the "center point" of a hatch, you have to find the number of loops that made the hatch boundary, and then extract the loops (as a polylines) one by one and examine the polyline to find if it's a circle or a triangle, the triangle one will have no bulges, only straight segments, and the "center" could be calculated by elemental geometry. As for the circle, you have to examine the bulges to find the radius of the circle, and find the center by usual geometry. Other approach could be to convert the loops in regions and find the centroid of each region.

 Gaston Nunez

gile

  • Gator
  • Posts: 2507
  • Marseille, France
It seems to me the center point is the center of the hatch bounding box (GeometricExtents).
Speaking English as a French Frog

waterharbin

  • Guest
Hi, Gasty
Thanks for replying, but your answer scares me, is it that complicate!
Hi, gile
The center of the bounding box is also cool, but what is the bounding box? How to get the bounding box or its center point?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
All entities have a GeometricExtents property that will give you what you need to calculate the centre given a min and max point from the returned Extents3d object.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

waterharbin

  • Guest
Hi, Mick
Thanks a lot, the MaxPoint and MinPoint can help to get the center point.

But I have a further question: is there any way to tell these two kinds of hatches apart?

WILL HATCH

  • Bull Frog
  • Posts: 450
Look at the NumberOfLoops property

MexicanCustard

  • Swamp Rat
  • Posts: 705
All entities Resident in the Database have a GeometricExtents property that will give you what you need to calculate the centre given a min and max point from the returned Extents3d object.

MickD, just to let everyone know of an exception I ran across. If you create objects for purposes other than adding to the database. Some objects will have a null GeometricExtents property.  I know DbText objects are one of these.
Revit 2019, AMEP 2019 64bit Win 10

fixo

  • Guest
All entities Resident in the Database have a GeometricExtents property that will give you what you need to calculate the centre given a min and max point from the returned Extents3d object.

MickD, just to let everyone know of an exception I ran across. If you create objects for purposes other than adding to the database. Some objects will have a null GeometricExtents property.  I know DbText objects are one of these.
This should work but with ellipses and splines
Code - C#: [Select]
  1.         [CommandMethod("had")]
  2.         public void hatch_and_text()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             ObjectIdCollection htids = new ObjectIdCollection();
  8.             ObjectIdCollection txtids = new ObjectIdCollection();
  9.            // List<string> texts = new List<string>();
  10.             Dictionary<ObjectId, string> texts = new Dictionary<ObjectId, string>();
  11.             try
  12.             {
  13.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  14.                 {
  15.                     SelectionFilter filt = new SelectionFilter(new TypedValue[]
  16.                     { new TypedValue(-4, "<OR"),
  17.                         new TypedValue(0, "hatch"),
  18.                         new TypedValue(0, "text"),
  19.                         new TypedValue(-4, "OR>")
  20.                     });
  21.                     SelectionFilter txtfilt = new SelectionFilter(new TypedValue[]
  22.                     { new TypedValue(0, "text")
  23.                     });
  24.                     PromptSelectionOptions pso = new PromptSelectionOptions();
  25.  
  26.                     pso.MessageForRemoval = "\nMust be selected the Select hatches and / or texts!";
  27.  
  28.                     pso.MessageForAdding = "\nSelect hatches and texts: ";
  29.  
  30.                     SelectionSet htSet = ed.GetSelection(filt).Value;
  31.  
  32.                     foreach (SelectedObject selobj in htSet)
  33.                     {
  34.                         if (selobj.ObjectId.ObjectClass != RXClass.GetClass(typeof(Hatch)))
  35.                         {
  36.  
  37.                             continue;
  38.                         }
  39.                         else
  40.                         {
  41.                             htids.Add(selobj.ObjectId);
  42.                         }
  43.                         if (selobj.ObjectId.ObjectClass != RXClass.GetClass(typeof(DBText)))
  44.                         {
  45.  
  46.                             continue;
  47.                         }
  48.                         else
  49.                         {
  50.                             txtids.Add(selobj.ObjectId);
  51.                         }
  52.  
  53.                         /*
  54.                         //if (selobj.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Hatch))))
  55.                         //{
  56.                         //    htids.Add(selobj.ObjectId);
  57.                         //}
  58.                         //else if (selobj.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText))))
  59.                         //{
  60.                         //    txtids.Add(selobj.ObjectId);
  61.                         //}// good catch too*/
  62.                     }
  63.                     foreach (ObjectId id in htids)
  64.                     {
  65.                         Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
  66.                         Hatch ht = ent as Hatch;
  67.                         if (ht != null)
  68.                         {
  69.                             int nloops = ht.NumberOfLoops;
  70.  
  71.                             for (int i = 0; i < nloops; i++)
  72.                             {
  73.                                 HatchLoop loop = ht.GetLoopAt(i);
  74.  
  75.                                 if (loop.IsPolyline)
  76.                                 {
  77.                                     using (Polyline poly = new Polyline())
  78.                                     {
  79.                                         int iVertex = 0;
  80.                                         foreach (BulgeVertex bv in loop.Polyline)
  81.                                         {
  82.                                             poly.AddVertexAt(iVertex++, bv.Vertex, bv.Bulge, 0.0, 0.0);
  83.                                         }
  84.  
  85.                                         Point3dCollection tmppts = new Point3dCollection();
  86.                                        
  87.                                             for (int j = 0; j < poly.NumberOfVertices; j++)
  88.                                             {
  89.                                                 Point3d p = poly.GetPoint3dAt(j);
  90.                                      
  91.                                                 tmppts.Add(p);
  92.                                             }
  93.                                    
  94.                                         PromptSelectionResult txtres = ed.SelectCrossingPolygon(tmppts, txtfilt);//or SelectWindowPolygon to select inside
  95.                                         if (txtres.Status == PromptStatus.OK)
  96.                                         {
  97.                                             foreach (SelectedObject txtobj in txtres.Value)
  98.                                             {
  99.                                                 Entity txtent = (Entity)tr.GetObject(txtobj.ObjectId, OpenMode.ForRead);
  100.                                                 DBText txt = txtent as DBText;
  101.                                                 if (txt != null)
  102.                                                 {
  103.                                                     texts.Add(txt.ObjectId,txt.TextString);
  104.                                                 }
  105.                                             }
  106.                                         }
  107.  
  108.                                     }
  109.                                 }
  110.  
  111.                             }
  112.                         }
  113.                     }
  114.                     //rest your work with texts here
  115.                     foreach (KeyValuePair<ObjectId, string> kv in texts)
  116.                     {
  117.                         ed.WriteMessage("\n{0} :\t{1}", kv.Key, kv.Value);
  118.                     }
  119.                     tr.Commit();
  120.                 }
  121.             }
  122.             catch (System.Exception ex)
  123.             {
  124.                 ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
  125.             }
  126.             finally
  127.             {
  128.                 if (texts.Count == 0)
  129.                 {
  130.                    
  131.                         ed.WriteMessage("\nDictionary is empty");
  132.                 }
  133.             }
  134.         }
  135.  

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
All entities Resident in the Database have a GeometricExtents property that will give you what you need to calculate the centre given a min and max point from the returned Extents3d object.

MickD, just to let everyone know of an exception I ran across. If you create objects for purposes other than adding to the database. Some objects will have a null GeometricExtents property.  I know DbText objects are one of these.

Good point!
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien