Solidworks API. Move face

Hello everyone.

 

Wishing to create a macro in order to automate the creation of a function to move face => to the surface + subsequently other function, I wrote the following code.

However, the function is not created and I really don't see the why or how. I have selected my starting and finishing faces with the required marks but nothing is created.

Do you have an idea??

 

Dim swApp As Object
Dim swmodel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature
Dim swMoveFaceFeat As SldWorks.MoveFaceFeatureData

Sub main()

Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc


'Verify that the open file is a part file
If swmodel. GetType <> swDocPART Then
    MsgBox "Please open a part file", vbApplicationModal + vbOKOnly + vbCritical, "Part file required"
    Exit Sub
End If


Dim Selectmanager As SelectionMgr
        Set Selectmanager = swmodel. SelectionManager' recover selection
        swmodel. ClearSelection2 True ' Clear Selection
        
        

' Loop for waiting selection
            Dim bool1 As Boolean
            Sun y
            bool1 = False
            Do Until bool1 = True
                If Selectmanager.GetSelectedObjectType3(1, -1) = 2 Then
                bool1 = True
                End If
                For y = 1 To 50000
                DoEvents
                Next y
            Loop

        Dim coord as variant
        Coord = Selectmanager.GetSelectionPointInSketchSpace2(1, -1)
        
        Dim x0 As Single
        Sun y0 As Single
        Dim z0 As Single
        
        x0 = Coord(0)
        y0 = Coord(1)
        z0 = Coord(2)
        
        swmodel. ClearSelection2 (True)
       
        
    Set swModelDocExt = swmodel. Extension
    Set swFeatMgr = swmodel. FeatureManager


        
        swmodel. ClearSelection2 (True)
        
        ' Loop for waiting selection
            
            bool1 = False
            Do Until bool1 = True
                If Selectmanager.GetSelectedObjectType3(1, -1) = 2 Then
                bool1 = True
                End If
                For y = 1 To 50000
                DoEvents
                Next y
            Loop
        Coord = Selectmanager.GetSelectionPointInSketchSpace2(1, -1)
        x1 = Coord(0)
        y1 = Coord(1)
        z1 = Coord(2)
        swmodel. ClearSelection2 (True)
        bret = swmodel. Extension.SelectByID2("", "FACE", x0, y0, z0, True, 1, Nothing, 0)
        bret = swmodel. Extension.SelectByID2("", "FACE", x1, y1, z1, True, 8, Nothing, 0)
      
      Set swFeat = swFeatMgr.InsertMoveFace3(swMoveFaceTypeTranslate, False, 0, 0, Nothing, Nothing, swEndConditions_e.swEndCondUpToSurface, 0)


End Sub
 

 

Thank you in advance

 

Happydad

stupid question

But why this type of macro??????????????????????

I don't understand !!!!!!!!!!!

1 Like

Simply because in my company we have a part file library that serves as a modeling base via the insert => part command.

We insert these elements into the space and then link them to each other. The number of library elements can be quite large, so we wanted to make a macro in order to autoatize this task somewhat.

a bit in the same spirit as routing

See this link

http://help.solidworks.com/2015/French/SolidWorks/sldpiping/c_connection_route_points.htm?id=80ee80a724b34d0e8281b0af48e1a92d#Pg0&ProductType=&ProductName=

Indeed a bit in the same spirit but we only have a few premuim licenses so I have to stay on the basic functions of SW.

 

1 Like

and if by chance you created your pieces that face it to move between 2 planes

you will just have to change the offset length of these 2 planes

I don't know if I'm making myself understand 

The trick would be to know what type of part should have a front offset and in relation to what

@+

If I understand correctly, you're talking to me about a part in an assembly. Am I wrong?

Now I talk about this in a document file. We have a part file in which we insert other part files that serve as a basis for construction.

To be as complete as possible, we do welded mechanics (with our own profiles) and we reconstruct the whole of a part (head file) from the profile connections that are established by the R&D department.

See attached screenshot if it can be more telling.

 


sans_titre.png

Hello

If I understood correctly the method used (which I can't test since I'm on SW2014), there are some parameters missing in the last line. In this case, since you put nothing in the movement settings, it's normal that nothing happens.

You have to define the X,Y,Z parameters and probably the direction of the function.

That's what I also told myself at the beginning I had created a variable of type variant to give my X, Y and Z values in the function but it didn't change anything.

I admit that I have a hard time interpreting this part of the API help. Because between the values not used since I'm in condition up to the surface and not blind, I tend to think that it handles the translation value like a big one since it has a starting face and an ending face (dito as if we were doing the function manually.)

edit Replacement of the help excerpt with a link to the online help

http://help.solidworks.com/2015/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IFeatureManager~InsertMoveFace3.html

Hello

I'm not sure I understood, but to create an associativity, the function move face can very well be associated with a reference plane by choosing translate instead of offset. After that, it would simply be enough to add a property to change the value of the offset, or create the plane according to a variable reference (distance origin, body face, ...).

 

That's why I want to use the condition up to the surface, which in case of variation will always follow elements.

I tested in the meantime to modify the macro, if I give it a blind end condition with values in the insertmoveface3 function it is fine, however the end condition is not the one that would meet our expectations.

Previously (before moving to SW 2015, we did sketch extrusions converted to the surface of the next body)

After many tests this solution is faster to implement and above all much less time-consuming in reconstruction time)

 

I think we need to add a selection for the orientation of the function (paragraph remarks) because of the use of the swMoveFaceTypeTranslate function. In the selctionbyID2 you need a selection with the mark parameter set to 2.

To be tested.

This is one of the solutions I tested, and unfortunately it didn't change anything.

 

 

Ok I understood, indeed  it's complicated, how can you define an automatic orientation according to the number of vertical and horizontal fittings ? It's a project that is beyond my skills, I can't help you dsl. Otherwise using the boss / base border function wouldn't be easier?

Hello everyone, after very long trials, here is the solution 

 

Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
Set swFrame = swApp.Frame
Set swModelDocExt = swmodel. Extension
Set swFeatMgr = swmodel. FeatureManager
z = 0
nb_profil = 0
'Verify that the open file is a part file
If swmodel. GetType <> swDocPART Then
    MsgBox "Please open a part file", vbApplicationModal + vbOKOnly + vbCritical, "Part file required"
    Exit Sub
End If

MsgBox "Select the starting face, then the end face to generate the link" & vbLf & "Repeat as many times as necessary" & vbLf & vbLf & "To switch to merging the bodies press the F2 key", vbInformation + vbApplicationModal + vbOKOnly, "Creating the functions move face"

Selection:
Dim Selectmanager As SelectionMgr
        Set Selectmanager = swmodel. SelectionManager' recover selection
        swmodel. ClearSelection2 True ' Clear Selection
' Loop for waiting selection
    Do Until Selectmanager.GetSelectedObjectType3(1, -1) = SwConst.swSelFACES or GetAsyncKeyState(113) <> 0
      DoEvents
      swFrame.SetStatusBarText "Please select the first side or press the F2 key to proceed to creating the combine functions" ' display of the message in the SW bar
    Loop
Dim swFace As SldWorks.Face2
Debug.Print GetAsyncKeyState(112)
Debug.Print GetAsyncKeyState(123)
nb = Selectmanager.GetSelectedObjectCount2(-1)
If nb <> 0 Then
    Set swFace = Selectmanager.GetSelectedObject6(1, -1)
    Dim swEntity As SldWorks.Entity
    Set swEntity = swFace
    Set body = swFace.GetBody
    profile = body. Name
    nb_car = (Len(profile) - Len(Replace(profile, "<", "", , , 0))) / Len("<")
        If nb_car <> 0 Then
             profile = Left(profile, (Len(profile) - 1))
             profile = "_" & Split(Split(Split(profile, "<")(nb_car), ">")(0), " ")(0) & "_" & nb_profil
             bodysuit. Name = profile
            nb_profil = nb_profil + 1
        End If
Else
    swmodel. ClearSelection2 True
    GoTo combine
End If

swmodel. ClearSelection2 True

' Loop for waiting selection
    While Selectmanager.GetSelectedObjectType3(1, -1) <> SwConst.swSelFACES
      DoEvents
      swFrame.SetStatusBarText "Please select the second side" ' display of the message in the SW bar
    Wend
    
Set swFace = Selectmanager.GetSelectedObject6(1, -1)

swmodel. ClearSelection2 True
    
swEntity.SelectByMark True, 1
swEntity.SelectByMark True, 2
Set swEntity = swFace
swEntity.SelectByMark True, 8
Set body = swFace.GetBody
profile = body. Name
nb_car = (Len(profile) - Len(Replace(profile, "<", "", , , 0))) / Len("<")
If nb_car <> 0 Then
    profile = Left(profile, (Len(profile) - 1))
    profile="_" & Split(Split(Split(profile, "<")(nb_car), ">")(0), " ")(0) & "_" & nb_profil
    bodysuit. Name = profile
    nb_profil = nb_profil + 1
End If

Creating the function Move Face + Checking Creation
Set swFeat = swFeatMgr.InsertMoveFace3(swMoveFaceTypeTranslate, False, 0, 0, Nothing, Nothing, 3, 0)
If swFeat Is Nothing Then
    Set swFeat = swFeatMgr.InsertMoveFace3(swMoveFaceTypeTranslate, True, 0, 0, Nothing, Nothing, 3, 0)
    If swFeat Is Nothing Then
        Select Case MsgBox("Couldn't create the binding function" & vbLf & vbLf & "Do you want to retry the link?", vbApplicationModal + vbCritical + vbYesNo, "Error Building")
        vbYes box
            GoTo selection
        Box vbNo
        End Select
    Else
        Select Case MsgBox("Do you want to create an additional binding?", vbApplicationModal + vbCritical + vbYesNo, "Error generating it")
        vbYes box
            GoTo selection
        Box vbNo
        End Select
    End If
Else
z = z + 1
ReDim Preserve feat_name(z)
feat_name(z) = swFeat.Name
GoTo selection
End If