3 Replies Latest reply on Mar 10, 2015 11:53 AM by jason van clark

    get the edges associated with a hole table

    jason van clark

      does anyone know how to get the edges/entities used in a hole table?  I am trying to automatically split a hole table into multiple tables by size of hole



        • Re: get the edges associated with a hole table
          Keith Rice

          Doesn't look like it. Though I am not sure why, in order to accomplish your goal, you would need to have those edges.


          This wouldn't be parametric, but you might simple extract all of the data, sort it programmatically, and then create new a general table with the data "hard-coded" in. My Macro Library has a macro called "Get cell data in active sheet's table" that will get the cell data from a BOM table but you can adapt it easily enough for a hole table.



          SolidWorks API Tutorials

          • Re: get the edges associated with a hole table
            jason van clark

            Well, the code works but it is kind of ugly.  Here's the gist of it:

            The user selects a table

            I verify it is a hole table

            I loop through the table and store the tag and size of each row in a list of custom objects

            The table origin is stored as an entity

            I get the hole table feature's parent and select it, then store it in a view object

            I get a collection of the notes on the view and store them in a list

            I loop through the notes collection and compare them to the list of tags/sizes.  if the tag and note text match i store the associated edge

            From there it is a lot of sorting, deleting and creating of tables.


            Here's the code




                            ModelDoc2 swDraw;

                            DrawingDoc Drawing;

                            Sheet swSheet;

                            double height = 0;

                            double width = 0;




                            swDraw = iSwapp.ActiveDoc; //get the active document

                            Drawing = iSwapp.ActiveDoc;

                            swSheet = (Sheet) Drawing.GetCurrentSheet();

                            swSheet.GetSize(ref width, ref height); //get the size of the sheet



                            height = height - .015; //set the height location of our tables



                            if (swDraw == null) //if we can't get an active document then dump the code





                            TableAnnotation swTable = null;

                            SelectionMgr swMgr;

                            bool status;


                            swMgr = swDraw.SelectionManager; //get the selection manager for the document



                            swTable = swMgr.GetSelectedObject6(1, -1); //get the selected table



                            if (swTable == null)


                                MessageBox.Show("You must select a hole table");

                                return;//if the table is nothing we can't keep going




                            swTableAnnotationType_e tableType = (swTableAnnotationType_e) swTable.Type; //get the type of table



                            switch (tableType) //figure out what kind of table we have


                                case swTableAnnotationType_e.swTableAnnotation_HoleChart:

                                    //keep going because we have a hole table





                                    MessageBox.Show("You must select a hole table");

                                        //tell the user they need a hole table selected

                                    return; //end the code




                            Cursor.Current = Cursors.WaitCursor;

                            int rowCount;



                            List<HoleInfo> localHoleInfo = new List<HoleInfo>();



                            rowCount = swTable.RowCount;



                            for (int i = 1; i < rowCount; i++)




                                localHoleInfo.Add(new HoleInfo(swTable.get_DisplayedText(i, 0), swTable.get_DisplayedText(i, 3)));




                            var holeSizes = new List<string>();



                            foreach (var item in localHoleInfo)


                                if (!holeSizes.Contains(item.Size))







                            HoleTableAnnotation myHoleTableAnn = (HoleTableAnnotation)swTable; //convert to a hole table annotation

                            HoleTable myHoleTable;

                            object[] notesCollection;

                            Feature tableFeature;

                            Feature parent;

                            View activeView;

                            bool result;

                            DatumOrigin tableOrigin;

                            Annotation originAnnotation;

                            object[] originObject;

                            Entity originEntity;

                            //Vertex originVert;



                            myHoleTable = myHoleTableAnn.HoleTable; //get the hole table object



                            tableOrigin = myHoleTable.DatumOrigin; //get the table origin



                            originAnnotation = tableOrigin.GetAnnotation(); //get the annotation of the origin



                            originObject = originAnnotation.GetAttachedEntities3(); //get the object that the origin is attached to



                            originEntity = (Entity)originObject[0]; //get an entity of the object the origin is attached to

                            //originVert = (Vertex)originObject[0];



                            tableFeature = myHoleTable.GetFeature(); //get the hole table feature



                            parent = tableFeature.GetOwnerFeature(); //get the view that the hole table is based on


                            result = parent.Select2(false, -1); //select the view so we can get an object of it



                            if (!result) return; //jump out of the code if we can't select the view



                            activeView = swMgr.GetSelectedObject6(1, -1); //get the object of the selected view



                            notesCollection = activeView.GetNotes(); //get all of the notes on the view



                            foreach (Note temp in notesCollection) //loop through the notes and get all of them


                                if (localHoleInfo.Any(e => e.Tag == temp.GetText())) //see if the hole table contaings the text of this note


                                    Annotation tempAnn;

                                    object[] annEdges;

                                    Edge tempEdge;



                                    tempAnn = temp.GetAnnotation(); //get the annotation associated with the note



                                    annEdges = tempAnn.GetAttachedEntities3(); //get the edge of the annotation



                                    tempEdge = (Edge)annEdges[0]; //convert the object into an edge



                                    //Edges.Add(temp.GetText(), tempEdge); //store the edge and the note text in the dictionary



                                    localHoleInfo.FirstOrDefault(e => e.Tag == temp.GetText()).entEdge = tempEdge;//store the edge in the localHoleInfo list





                            //here's the magic: we're going to split the hole table based on the number of distinct hole sizes in the original table

                            tableFeature.Select2(false, -1); //select the existing table

                            swDraw.EditDelete(); //delete the table



                            double widthMultiplier = 0;



                            foreach (string size in holeSizes) //loop through the different hole sizes in the list.  this tells us how many tables to create


                                double xPos;



                                xPos = widthMultiplier*.15;

                                xPos = xPos + .015;



                                string startTag = "";

                                List<HoleInfo> localHoleList = new List<HoleInfo>();//create a new list to store the holes with the current size


                                foreach (var item in localHoleInfo)//loop through the localHoleInfo list to find the holes that have the current size


                                    if (item.Size == size) //if the sizes are the same then add it to our temporary list



                                        startTag = item.Tag.Substring(0, 1);//read the first character in the tag to determine our start tag





                                HoleTableAnnotation newTable; //make a new hole table



                                swDraw.ClearSelection2(true); //clear the selection list



                                Drawing.ActivateView(activeView.Name); //activate the view the table will be attached to



                                result = originEntity.Select2(true, 1); //select the origin


                                foreach (var item in localHoleList)//loop thorugh the temporary list and select all of th hole edges


                                    Entity temp = (Entity)item.entEdge; //convert the edge to an entity



                                    result = temp.Select2(true, 2); //select the entity




                                //make a table

                                newTable = activeView.InsertHoleTable2(false, xPos, height, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, startTag, "C:\\Program Files\\SolidWorks Corp\\2013\\SolidWorks\\lang\\english\\standard hole table--letters.sldholtbt");



                                HoleTable newHoleTable = newTable.HoleTable;//get teh hole table



                                newHoleTable.CombineSameSize = true; //combine the same sizes



                                widthMultiplier = widthMultiplier + 1;



                            }//foreach (string size in holeSizes)



                            Cursor.Current = Cursors.Default;//set the cursor back to default


                        catch (Exception)


                            MessageBox.Show("There was an error");

                            Cursor.Current = Cursors.Default;