8 Replies Latest reply on Oct 24, 2018 7:05 PM by Gennadiy Tsybikov

    component2.name2 doesn't return hierarchy

    Gertjan Van Dijk

      I am working on reworking out companies' in-house solidworks toolbar to a newer, easier to maintain version, but i am being stumped by a specific API call.

       

      One process in the toolbar requires me to select the origin of a component X levels deep into subassemblies on a drawing, and since i want to attach a Balloon to it, this needs to happen with "SelectByID2".

       

      now; to create the selection string, i have a small utility method that is used on several locations:

       

      [quote]

      StringBuilder builder = new StringBuilder("Point1@Origin@");

      builder.Append(swView.RootDrawingComponent.Name);

      builder.Append("@" + swView.Name);

       

       

      string[] fileStructure = comp.Name2.Split('/');

       

       

      builder.Append("/" + fileStructure[0]);

       

       

      builder.Append("@" + swView.RootDrawingComponent.Name.Split('-')[0]);

       

       

      for (int i = 1; i < fileStructure.Count(); i++)

      {

      builder.Append("/" + fileStructure[i] + "@");

      builder.Append(fileStructure[i - 1].Split('-')[0]);

      }

      return builder.ToString();

      [/quote]

       

      The problem in this method lies in  "Component2::Name2" it only returns the components' own name with the indexer, not the entire hierarchy that the API documentation claims it should return.

      I would expect the call to return "Assem4-1/phantom-1/Part1-1", but i only get "Part1-1".

       

      I have confirmed that i indeed am referencing the correct component.

       

      Trying to build my own full name, i called "Component2::GetParent", but the debugger claims that this method does not exist, despite suggesting it during autocomplete.

      To make things even more interesting, "Component2::GetSelectByIDString" does realize that the current component is in an assembly, as it returns "Part1-1@phantom", but it fails to realize that "phantom" itself is a subassembly.

       

      Even stranger is the fact that this exact same call in VB.Net (same assembly, same references, same method of reaching the component) i fact does produce the desired and expected result of "Assem4-1/phantom-1/Part1-1"

       

       

       

       

      The solidworks.interop.sldworks dll version in use is 25.2.0.52; i am unable to change this, and the language in use is C#; this also won't change.

       

      Does anyone have any insight over the difference between the languages, or a different method of attaching notes? (i tried component.select, this is not accepted by the API)

        • Re: component2.name2 doesn't return hierarchy
          Fifi Riri

          Hello,try :

          comp.GetPathName

          • Re: component2.name2 doesn't return hierarchy
            Josh Brady

            "Trying to build my own full name, i called "Component2::GetParent", but the debugger claims that this method does not exist, despite suggesting it during autocomplete."

             

            This makes me think that you have some problem with variable name conflict.  You think you're dealing with a Component2, but really you have a DrawingComponent.

             

            You haven't posted enough of your code to figure out what the problem is... Plus I don't do C anyway. 

             

            This code may present an easier way to achieve your SelectByID2 string...

             

            Preconditions: A drawing is open whose first view is an assembly with at least one subassembly.

            Postconditions: The origin of each component at the first and second levels are selected.

             

            Dim swApp As SldWorks.SldWorks
            Dim swAssy As SldWorks.AssemblyDoc
            Dim swdoc As SldWorks.ModelDoc2
            Dim swDwg As SldWorks.DrawingDoc
            Dim swView As SldWorks.View
            Dim swComp As SldWorks.Component2
            Dim swDwgComp As SldWorks.DrawingComponent
            
            Sub fromDwg()
            Set swApp = Application.SldWorks
            Set swdoc = swApp.ActiveDoc
            Set swDwg = swdoc
            Set swView = swDwg.GetFirstView
            Set swView = swView.GetNextView
            Debug.Print swView.Name
            Set swDwgComp = swView.RootDrawingComponent
            Debug.Print swDwgComp.Name
            Dim aComps As Variant
            Dim aComps2 As Variant
            aComps = swDwgComp.GetChildren
            Dim i As Long
            Dim j As Long
            For i = 0 To UBound(aComps)
                Set swDwgComp = aComps(i)
                Debug.Print swdoc.Extension.SelectByID2("Point1@Origin@" & swView.RootDrawingComponent.Name & "@" & swView.Name & "/" & aComps(i).Component.GetSelectByIDString, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
                aComps2 = swDwgComp.GetChildren
                Debug.Print , swDwgComp.Name
                For j = 0 To UBound(aComps2)
                    Debug.Print , , aComps2(j).Name
                    Debug.Print swdoc.Extension.SelectByID2("Point1@Origin@" & swView.RootDrawingComponent.Name & "@" & swView.Name & "/" & aComps2(j).Component.GetSelectByIDString, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
                Next j
            Next i
            
            
            End Sub
            
              • Re: component2.name2 doesn't return hierarchy
                Gertjan Van Dijk

                That might work indeed; although it is not too different from what my snippet is (trying to) achieve.

                 

                I deliberately held back some code in order to protect ID, but i will add some more

                 

                I made sure to get the actual Component2 at the start of the feature tree traversal, using swDraw.RootDrawingComponent.Component

                Due to the requirements of the process, i have to guarentee the order of appearance of the child components; so i traverse the root with Feature:NextFeature, checking if the feature is of a part or an assembly, and casting that into a Component2

                 

                void Traverse(Component2 assyComp)

                {

                Feature swFeat = assyComp.FirstFeature();

                while (swFeat != null)

                {

                if (swFeat.GetTypeName2().ToLower() == "reference")

                {

                swFeat.Select(false);

                 

                childComp = selMgr.GetSelectedObjectsComponent4(1, -1);

                assyMod.ClearSelection2(true);

                EvaluateChild(childComp);

                }

                swFeat = swFeat.GetNextFeature();

                }

                }

                 

                In "EvaluateChild", i check several properties of the component, and in some situations, i recursively call back into 'Traverse' to check the children.

                 

                Void EvaluateChild(Component2 child)

                {

                     Enum outcome = DoChecks()

                    

                     switch(outcome)

                          //irrelevant cases

                          Case Tag:

                               Tag(child)

                          default

                               Traverse(child)

                }

                 

                "Tag" is where the selectstring is needed, but since i have no way of predicting the depth level of traversal (it is at least 3 levels in practice: main -> sub ->subsub), i need an universal snippet that always works, between 1 and 10 levels, to be on the safe side.

                  • Re: component2.name2 doesn't return hierarchy
                    Artem Taturevych

                    I'm not exactly sure what is your workflow, but as far as I understand you want to assign some tags/attributes to the components for these to be later identified. Take a look at this article: Tracking Objects . You might have better option of doing this instead of names (such as persist references or attribute).

                      • Re: component2.name2 doesn't return hierarchy
                        Gertjan Van Dijk

                        Almost.

                        The tags and attributes have already been assigned to the models by the construction engineer (or from our default parts library).

                        The "Tag()" method places a note on the drawing, attached to the origin of the part, with some identifying text on it.

                         

                        The process in question is the last step before sending the design to production, and marks the drawing to make the physical production easier.

                         

                        The short version is: I have found a part inside of a subassembly, and now i want to select its' origin on the drawing using SelectByID2.

                        I know the partname, and i know the viewname, i just don't know the names of the assemblystack that are needed to create the selectionstring.

                          • Re: component2.name2 doesn't return hierarchy
                            Josh Brady

                            Well... I dunno how it works for you, because the following code doesn't even return a feature for the root drawing component first feature.  As far as I can read, I've done in VBA the same thing you're saying you're doing, but this is as far as it'll get.

                             

                            Dim swApp As SldWorks.SldWorks

                            Dim swAssy As SldWorks.AssemblyDoc

                            Dim swdoc As SldWorks.ModelDoc2

                            Dim swDwg As SldWorks.DrawingDoc

                            Dim swView As SldWorks.View

                            Dim swDwgComp As SldWorks.DrawingComponent

                             

                             

                             

                             

                            Sub FindTheFirstFeature()

                            Set swApp = Application.SldWorks

                            Set swdoc = swApp.ActiveDoc

                            Set swDwg = swdoc

                            Set swView = swDwg.GetFirstView

                            Set swView = swView.GetNextView

                            Debug.Print swView.Name

                            Set swDwgComp = swView.RootDrawingComponent

                            Debug.Print swDwgComp.Component.Name2

                            Debug.Print swDwgComp.Component.FirstFeature Is Nothing

                            End Sub

                    • Re: component2.name2 doesn't return hierarchy
                      Gennadiy Tsybikov

                      Try the example below. I just commented out "Hide Component" line. It prints out in the debug window the component name with entire hierarchy.

                      You can find the example here: 2017 SOLIDWORKS API Help - Hide Drawing Components Example (VBA)

                       

                      Option Explicit
                      
                      
                      Sub ProcessDrawingComponent(swDrawComp As SldWorks.DrawingComponent, sPadStr As String)
                          Dim vDrawCompChildArr As Variant
                          Dim vDrawCompChild As Variant
                          Dim swDrawCompChild As SldWorks.DrawingComponent
                          Debug.Print sPadStr & swDrawComp.Name
                          ' Does not affect root component
                          'swDrawComp.Visible = False
                          vDrawCompChildArr = swDrawComp.GetChildren
                          If Not IsEmpty(vDrawCompChildArr) Then
                              For Each vDrawCompChild In vDrawCompChildArr
                                  Set swDrawCompChild = vDrawCompChild
                                  ProcessDrawingComponent swDrawCompChild, sPadStr + "  "
                              Next
                          End If
                      End Sub
                      
                      
                      Sub main()
                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim swDraw As SldWorks.DrawingDoc
                          Dim swSelMgr As SldWorks.SelectionMgr
                          Dim swView As SldWorks.View
                          Dim swDrawComp As SldWorks.DrawingComponent
                          Dim swComp As SldWorks.Component2
                          Dim bRet As Boolean
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          Set swDraw = swModel
                          Set swSelMgr = swModel.SelectionManager
                          Set swView = swSelMgr.GetSelectedObject6(1, -1)
                          Set swDrawComp = swView.RootDrawingComponent
                          Debug.Print "File = " & swModel.GetPathName
                          Debug.Print "  " & swView.Name & "  [" & swView.Type & "]"
                          ProcessDrawingComponent swDrawComp, "    "
                      End Sub