Macro Linear Motor Insertion in Animation

Hello

I'm trying to get the next macro to work, but nothing happens.

Could someone explain it to me?

Thank you in advance.

http://help.solidworks.com/2015/english/api/sldworksapi/create_linear_motor_feature_example_vb.htm


create_linear_motor_feature.txt

First of all

Have you already done your assembly?

Is it open?

 

assembly made and opened, but nothing happens, just the movement study that opens

Have you defined the options as indicated on your link

 "Define options

    swMotorFeat. InterpolatedMotor swSimulationMotorDrive_Velocity, 1

    swMotorFeat. DirectionReference = swSelMgr.GetSelectedObject6 (1, -1)

    boolstatus = swMotorFeat. LoadSplineData ( «Test_bouncingBall.csv")

1 Like

not done indeed.... How do you do that?

well I think you have to define your engine, the speed, the direction etc........

I thought these lines would allow you to create the engine with the .csv file as a link to import the displacement data points, without having to create the engine first in the motion study.

Does this mean that there are missing lines in the macro or that the engine must already be created?

 look at what's noted here it's your link

  Activate Motion 3 Study

    swMotionStudy3. Activate

    

   

    'Select face on Part1

    boolstatus = swModelDocExt. SelectByID2 ( "", "FACE", -0.07792618280496, ,06212618843159, ,02214691016243, False, 0, Nothing, 0)

        

    'Create linear engine characteristic definition of the data object

    Set swMotorFeat = swMotionStudy3. CreateDefinition (swFmAEMLinearMotor)

    If swMotorFeat is nothing then

       Debug.Print "ERROR: Engine Characteristics Data Object Creation Failed."

        exit Sub

    Ends if

Watch this video in English 

https://youtu.be/rd6DqjREv-w

Hello

No, I think the engine is created by the macro.

Is the CSV file well enclosed?

Do you have the reference to the SolidWorks MotionStudy type library? (Read the Pre-Conditions note in the link)
 
Otherwise, reference by: SolidWorks> Tools> Macro> Edit, then in Visual Basic: Tools> References and check the library mentioned above.

Yes, for the .csv file and yes for references.

I put the CSV in the same folder as the assembly and the macro, but I don't specify the path, so I don't know if the macro will fetch the data?

The macro hangs at this line:

    ' Set motor feature's load references, the load bearing faces

    swMotorFeat.LoadReferances = vContact


with run-time error '438'

Property or method not supported by this object

 

maybe he can't find the contact face reference?

as mentioned above in my post partial copy

 ( 'Select face on Part1

    boolstatus = swModelDocExt. SelectByID2 ( "", "FACE", -0.07792618280496, ,06212618843159, ,02214691016243, False, 0, Nothing, 0)

        

    'Create linear engine characteristic definition of the data object

    Set swMotorFeat = swMotionStudy3. CreateDefinition (swFmAEMLinearMotor)

    If swMotorFeat is nothing then

       Debug.Print "ERROR: Engine Characteristics Data Object Creation Failed."

        exit Sub

    End if)

I think you need to select an object in the 3D view before launching the macro :

    Set ContactObj(0) = swSelMgr.GetSelectedObject6(1, -1)

1 Like

I think it comes from there as well, but I don't see anything abnormal in the selection of faces.

I'm thinking more about the CreateDefinition line for the linear engine

@PL.: Even if you select an object before launching the macro, it doesn't want to.

 

Here's the macro that works:

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Sun swSelMgr As SldWorks.SelectionMgr
Sun swMotionMgr As SwMotionStudy.MotionStudyManager
Dim swMotionStudy1 As SwMotionStudy.MotionStudy
Dim swMotorFeat As SldWorks.SimulationMotorFeatureData
Dim swGravityFeat As Object
Dim boolstatus As Boolean
Dim swFeat As SldWorks.Feature

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Set swModelDocExt = swModel.Extension
Set swSelMgr = swModel.SelectionManager

'---------
'Front Pallet
boolstatus = Part.Extension.SelectByID2("", "FACE", 0.116975825107659, 9.86277918692053E-02, -2.07497793580842E-02, False, 0, Nothing, 0)
Pallet origin
boolstatus = Part.Extension.SelectByID2("Point1@Origine@Part 1-2@toto-2", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
Origin
boolstatus = Part.Extension.SelectByID2("Point1@Origine", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
'-------------------

' Get the MotionManager
Set swMotionMgr = swModelDocExt.GetMotionStudyManager()
If (swMotionMgr Is Nothing) Then
End
End If

' Get Motion Study 1
Set swMotionStudy1 = swMotionMgr.GetMotionStudy("Motion Study 3")

' Enabling the Motion Study Tab 1
swMotionStudy1.Activate

' Creating the linear motor function as a data object
Set swMotorFeat = swMotionStudy1.CreateDefinition(swFmAEMLinearMotor)

' Interpolated displacement motor assignment
swMotorFeat.InterpolatedMotor swSimulationMotorDrive_Displacement, 0


' Management assignment
boolstatus = swModelDocExt.SelectByID2("", "FACE", 0.195285205513159, 4.90124177502054E-02, -3.98286386705422E-02, False, 0, Nothing, 0)
swMotorFeat.DirectionReference = swSelMgr.GetSelectedObject6(1, -1)


' Assigning the point of application of the engine
boolstatus = swModelDocExt.SelectByID2("Point1@Origine@Part 1-2@toto-2", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
swMotorFeat.Location = swSelMgr.GetSelectedObject6(1, -1)


' Assigning the Reference Object
boolstatus = swModelDocExt.SelectByID2("Part 1-1@toto-2", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Dim RelObj As SldWorks.Component2
Set RelObj = swSelMgr.GetSelectedObjectsComponent3(1, -1)
swMotorFeat.RelativeComponent = RelObj


'Loading the displacement data file
boolstatus = swMotorFeat.LoadSplineData("C:\CoordMX1.txt")


' Print the motor type
Debug.Print swMotorFeat.MotorType

' Create the linear motor feature
Set swFeat = swMotionStudy1.CreateFeature(swMotorFeat)

If swFeat Is Nothing Then

Debug.Print " ERROR: Creation of the motor feature failed."
Else
Debug.Print "Name of the feature added : " & swFeat.Name
End If

 

End Sub


Thanks for the help in making it work

In fact, in the macro, we see this:

  ' Select face on Part2

    boolstatus = swModelDocExt.SelectByID2("", "FACE", -0.07924982844941, 0.06212618843165, 0.03225592518596, False, 0, Nothing, 0)

    swMotorFeat.RelativeComponent = RelObj

    swMotorFeat.Location = swSelMgr.GetSelectedObject6(1, -1)

    ' Select same face on Part1 as previously selected

    boolstatus = swModelDocExt.SelectByID2("", "FACE", -0.07792618280496, 0.06212618843159, 0.02214691016243, False, 0, Nothing, 0)

Is this part working well?