9 Replies Latest reply on Aug 11, 2017 10:59 AM by Swme Eng

    HELP NEEDED: Trim Surface API

    Swme Eng

      Does anyone have experience trimming surfaces from the api?


      As far as I can tell from the severly lacking help file on the topic, the process for a mutual trim should look something like this:


      1. Pre-select surfaces that are going to be trimmed
        1. This is identical to how you would do it from the UI
      2. swFeatureManager.PreTrimSurface(True, True, False, True)
        1. This selects mutual trim, let solidworks decide what to trim, naturally extend surfaces, and removed selected surfaces (I think this is selections made after PreTrimSurface is called)
      3. Select surfaces near the sections that need to be removed
        1. Here is the tricky part, the example from the help files just has a static location. I am working on logic to find a point on the surface outside of one of the trimming surfaces. But for the mean time I found points in the the sections that I want to trim and hard coded locations like the example.
      4. Set swFeatureManager.SolidForTrim = False
        1. I think this is used when you want to knit the surfaces together and form a solid, someone correct me if I'm wrong about this.
      5. Set swFeature = swFeatureManager.PostTrimSurface(True)
        1. Not entirely sure what the True/False argument does, I think its supposed to knit surfaces is you select true.


      I have been working from this example:

      2016 SOLIDWORKS API Help - Create Solid Body Surface Trim Feature Example (VBA)


      I end up getting nothing when executing step 5.


      I have gotten standard trims to work, but then I would need to knit the surfaces and I can't get swFeatureManager.InsertSewRefSurface to work either. Have any of you tried to do something similar to this? Unfortunately I can't actually share the code or SolidWorks model that I am working with.


      I'm quite frustrated over the fact that I can't replicate performance with the exact syntax that the SW help files use. Also their method of selecting everything by name at a hard point is awful, you would never do that in reality it provides no flexibility for selecting objects using logic.


      Message was edited by: Trevor Dutrow

        • Re: HELP NEEDED: Trim Surface API
          Amen Allah Jlili

          Hey Trevor,


          You can select a surface body with SelectByID2 if you know the name of the feature.



            • Re: HELP NEEDED: Trim Surface API
              Swme Eng

              Amen, I've tried that and it seems like you have to select in the area where you want the surface removed. I plotted the points used in the example, and they are all in areas where the surface was removed. I think it is trying to replicate the behavior of how the command behaves in the UI. The hard part is once you call Pretrim any selections you make don't show up highlighted in the main SW window so there is now way to figure out if the selection worked, and there are no errors thrown if there is an issue, the feature just won't get created.

                • Re: HELP NEEDED: Trim Surface API
                  Amen Allah Jlili

                  Try recording a macro. Without code, I'm not sure how I can help.

                  • Re: HELP NEEDED: Trim Surface API
                    Simon Turner

                    To select a specific area on the surface, you can use the SelectData object

                    I assume it is a surface body and that you have a Body2 object which points to it (say swSurfBody). Then:

                    Dim selectData As SelectData

                    Dim selMgr As SelectionMgr

                    Set selMgr = swModel.SelectionManager

                    Set selectData  = selMgr.CreateSelectData

                    selectData.X = 100 / 1000    'Enter the x coordinate of the point where you want to select the surface body

                    selectData.Y = 200 / 1000    'Enter the y coordinate of the point where you want to select the surface body

                    selectData.Z = 300 / 1000    'Enter the z coordinate of the point where you want to select the surface body

                    selectData.Mark = 0 'Enter the "Mark" which you want to give your selection

                    swSurfBody.Select2 False, selectData

                      • Re: HELP NEEDED: Trim Surface API
                        Swme Eng

                        Thanks simon, let me give that a try.

                        • Re: HELP NEEDED: Trim Surface API
                          Swme Eng

                          YES!!!!! Success!!!


                          So it turns out that I was selecting faces and not surface bodies before calling pretrim. While that worked intermittently for standard (non-mutual) trims, it seems that mutual trims require selecting surface bodies first, using swsurfbody.select2(False, Nothing), then calling pretrim, then select each of the surfaces in the area to be removed using swsurfbody.select2(True, selectdata).


                          Now onto the logic that determines the XYZ coordinate data automatically instead of taking a hard coded position.



                          Thank you both for your help, this is a big step forward and has been holding me up for about a month of intermittent development.