6 Replies Latest reply on Mar 16, 2015 1:32 AM by Jason Bourne

    Reading multiple inputs for SelectByID2 from text file

    Jason Bourne

      Hello all,

       

      I am very new to VBA (but competent in Python) and would like some help on the following problem as I am having trouble writing a functioning macro for SolidWorks:

       

      I am setting up an automated workflow for the production of sheetmetal parts which are exported as 0 thickness polysurfaces from Rhinoceros as STEP files. I am also automating the export of a text file which identifies the fixed face, bend edges, thickness and radius parameters for each STEP file which I would like to convert into a SheetMetal Part in SW.

       

      Each text file looks like this:

      "FACE" 37.0334 3.6163 0.0

      "EDGE" 36.4862 3.6163 0.0

      "EDGE" 36.4862 3.3663 0.1724

      "EDGE" 37.5807 3.6163 0.0

      "EDGE" 37.0334 3.3663 0.0

       

      The corresponding VBA macro looks like this:

      Dim swApp As Object

      Dim part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Sub main()

      Set swApp = Application.SldWorks

      boolstatus = swApp.LoadFile2("C:\Users\userName\Desktop\Part1.stp", "r")

       

      Set part = swApp.ActiveDoc

      Open "C:\Users\g.maciocci\Desktop\FACE_EDGE_LIST.txt" For Input As #1

       

      Do While Not EOF(1)

      Input #1, SelType, X, Y, Z

      Set boolstatus = part.Extension.SelectByID2("", SelType, X, Y, Z, True, 0, Nothing, 0)     <----------- THIS IS WHERE I GET A TYPE MISMATCH ERROR THROWN

      Loop

      Close #1

       

      boolstatus = part.FeatureManager.InsertConvertToSheetMetal2(0.003, False, False, 0.003, 0.002, 0, 0.5, 0, 0.5, False)

      part.ClearSelection2 True

      longstatus = part.SaveAs3("C:\Users\userName\Desktop\Part1.SLDPRT", 0, 2)

      swApp.CloseDoc "Part1.SLDPRT"

       

      End Sub

       

      Now I know this is down to totally illiterate use of the VBA language!

       

      What I would like to do is take the SelType ("FACE" or "EDGE") from my text file and the x,y,z coordinates of the selection, pass them to SelectByID2 in order to define the fixed face and bend edges for the InsertConvertToSheetMetal2.

       

      I would also like to loop through a large number of STEP files and associated text files (defining fixed face and bend edges for each) converting to sheet metal, saving and closing each one.

       

      Could someone please help me debug the code?

        • Re: Reading multiple inputs for SelectByID2 from text file
          Keith Rice

          You shouldn't use IModelDocExtension::SelectByID2 to select faces; see the remarks in this API call for details. Ideally you would know the face area and normal and then search the body until you find a face that with that area and normal, and select it using IEntity::Select4.

           

          Keith

          SolidWorks API Tutorials

          • Re: Reading multiple inputs for SelectByID2 from text file
            Jason Bourne

            Keith,

             

            Thanks for your reply. Indeed selecting face by Area and Normal sounds like a much more reliable method. I am very new to SW and its API, and I'm just trying to work things out by recording macros and going through them.

             

            So, do you have any suggestions in regards to the edges? Would checking the start and end points be more reliable?

             

            The only thing I don't like about the above, however, is the fact that you have to iterate through all the faces and all the edges of the Part to find the correct ones by comparison. Using SelectByID2 and specifying the selection type as either EDGE or FACE seems to ensure the correct component is selected (but I haven't tested this extensively to know when and how it breaks).

             

            Best,

             

            -JB