Hi,
I need to create 144 trace paths so I can export the data, find the maximum distance from the center of all of the paths, and then re-import the data to create some geometries. Sound like fun? It isn't - it's completely mind numbing. And I want to automate it because I'm not sure we have the correct intial positions of all the bits that are being modeled - so I will probably need to do this several times. Yeah!
So here's the layout. I've got a platform (1) electronically cam'd to a turntable (2). Don't ask me how I got that to work - I can't repeat it, I've tried several times - but I have a file that works. On the platform is an oval. I've created 72 reference points around the oval that I want to trace the path of and a tooling ring (not shown). And why 144 points? Because the idiots in the product group decided to taper the oval on the platform. He's a pic of the layout without the tapered oval and a tooling ring that goes around the outside of everything.
From this post, https://forum.solidworks.com/message/976937?q=motion%20trace , I gathered you can't do a CSVExport on a trace path. So I need to create X & Y linear displacement plots for all of the points. I thought I would try replying to the aforementioned post to see if the guy with the original question had code that would create his plots but that would warp his original post and I'm pretty sure that's against some forum etiquette so now we're all here.
I've been beating my head against the sample code (Create Plots and Get Values Example)
trying to get it to take one of my 72 reference points on the oval relative to a center point of a tooling ring (not shown) that coincides with the center of the turntable. Here's what I've got so far:
'---------------------------------------------------------------------------
' Preconditions:
' 1. Start SOLIDWORKS Premium, which includes SOLIDWORKS Motion.
' 2. In SOLIDWORKS:
' a. Start the SOLIDWORKS Motion Study add-in (click Tools >
' Add-Ins > SOLIDWORKS Motion).
' b. Open install_dir\samples\tutorial\motionstudies\valve_cam.sldasm.
' c. Click the 1200 Motion Study tab in the lower-left corner of
' the MotionManager.
' d. Select Motion Analysis in the Type of Study list at the
' upper-left corner of the MotionManager.
' 3. In the IDE:
' a. Add a reference to the SOLIDWORKS Motion Study primary interop assembly
' (right-click the name of the project in the Project Explorer >
' click Add Reference > browse to install_dir\api\redist\CLR2 >
' click SolidWorks.Interop.swmotionstudy.dll).
' b. Open the Immediate window.
'
' Postconditions:
' 1. Selects two faces.
' 2. Calculates the motion analysis for the two selected faces.
' 3. Gets the translational displacement and translational velocity
' values for the first selected face.
' 4. Creates plots of the translational displacement and translational
' velocity.
' 5. Prints the names of the plot features to the Immediate window.
'
' NOTE: Because the assembly is used elsewhere, do not save changes.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.swmotionstudy
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public Sub Main()
Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swModelDocExt As ModelDocExtension
Dim MotionMgr As MotionStudyManager
Dim MotionStudy As MotionStudy
Dim MotionStudyResults As CosmosMotionStudyResults
Dim status As Boolean
Dim swSimPlotFeatureData As MotionPlotFeatureData
Dim swSimPlotXAxisFeatureData As MotionPlotAxisFeatureData
Dim swSimPlotYAxisFeatureData(0 To 1) As MotionPlotAxisFeatureData
Dim swFace(0 To 0) As Object
Dim swFaceArray() As Object
Dim swYAxisArray() As Object
Dim PlotOutput As MotionPlotFeatureOutput
Dim swPlotFeature1 As Feature
Dim swPlotFeature2 As Feature
Dim swXData As Object
Dim swYData As Object
Dim nameYAxis(0 To 1) As String
Dim i As Integer
Dim j As Integer
swModel = swApp.ActiveDoc
swSelMgr = swModel.SelectionManager
swModelDocExt = swModel.Extension
' Select the faces for which to calculate
' motion analysis, then calculate it and
' get the results
'swModel.ShowNamedView2("*Right", 4)
'status = swModelDocExt.SelectByID2("", "FACE", 0.03426699306681, 0.03342024416822, 0.02599934303839, True, 0, Nothing, 0)
'status = swModelDocExt.SelectByID2("", "FACE", 0.03047373686337, 0.006937653650944, 0.02566622869226, True, 0, Nothing, 0)
status = swModel.Extension.SelectByID2("Point121@tapered_oval@basic_asm_front_basis", "DATUMPOINT", 0, 0, 0, True, 0, Nothing, 0)
status = swModel.Extension.SelectByID2("Point1@Tooling Ring-1@basic_asm_front_basis", "DATUMPOINT", 0, 0, 0, True, 0, Nothing, 0)
MotionMgr = swModelDocExt.GetMotionStudyManager()
MotionStudy = MotionMgr.GetMotionStudy("Cubic 2")
'status = MotionStudy.Calculate
MotionStudyResults = MotionStudy.GetResults(4)
' Create a plot feature data and create the x and y axes feature data
swSimPlotFeatureData = MotionStudyResults.CreatePlotFeatureData()
swSimPlotXAxisFeatureData = MotionStudyResults.CreatePlotXAxisFeatureData()
swSimPlotYAxisFeatureData(0) = MotionStudyResults.CreatePlotYAxisFeatureData()
swSimPlotYAxisFeatureData(1) = MotionStudyResults.CreatePlotYAxisFeatureData()
' Set the type of plots
nameYAxis(0) = "swMotionPlotAxisType_TRANS_DISP"
swSimPlotYAxisFeatureData(0).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_DISP 'swMotionPlotAxisType_TRANS_DISP
swSimPlotYAxisFeatureData(0).Component = 0
nameYAxis(1) = "swMotionPlotAxisType_TRANS_DISP"
swSimPlotYAxisFeatureData(1).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_DISP
swSimPlotYAxisFeatureData(1).Component = 1
' Get the entity whose motion you want analyzed
swFace(0) = swSelMgr.GetSelectedObject6(1, -1)
swFaceArray = swFace
swSimPlotYAxisFeatureData(0).Entities = swFaceArray
swSimPlotYAxisFeatureData(1).Entities = swFaceArray
' Get the plot's x-axis and y-axes values
swYAxisArray = swSimPlotYAxisFeatureData
PlotOutput = MotionStudyResults.GetValues(swSimPlotFeatureData, swSimPlotXAxisFeatureData, (swYAxisArray))
'swXData = PlotOutput.GetXAxis()
'' Print the x-axis values and the y-axis translational
'' displacement values and the y-axes translational velocity
'' values to the Immediate window
'Debug.Print("")
'For i = 0 To UBound(swYAxisArray)
' Debug.Print("------ YAxis Type : " & nameYAxis(i))
' swYData = PlotOutput.GetYAxis(swSimPlotYAxisFeatureData(i))
' For j = 0 To UBound(swXData)
' Debug.Print(" (x, y) : (" & Strings.Format(swXData(j) & ", " & swYData(j) & ")"))
' Next j
'Next i
' Insert and display the translational displacement plot
swPlotFeature1 = MotionStudyResults.InsertPlotFeature(swSimPlotFeatureData, swSimPlotXAxisFeatureData, swSimPlotYAxisFeatureData(0))
Debug.Print("Name of plot feature: " & swPlotFeature1.Name)
' Insert and display the translational velocity plot
swPlotFeature2 = MotionStudyResults.InsertPlotFeature(swSimPlotFeatureData, swSimPlotXAxisFeatureData, swSimPlotYAxisFeatureData(1))
Debug.Print("Name of plot feature: " & swPlotFeature2.Name)
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class
I recorded a macro and boy was that disappointing. All I got out of it was here are the things you clicked on - which is where I got the 'SelectByID2' statements to point to my objects.
status = swModel.Extension.SelectByID2("Point121@tapered_oval@basic_asm_front_basis", "DATUMPOINT", 0, 0, 0, True, 0, Nothing, 0)
status = swModel.Extension.SelectByID2("Point1@Tooling Ring-1@basic_asm_front_basis", "DATUMPOINT", 0, 0, 0, True, 0, Nothing, 0)
For the 'Set type of plots' section, I changed both of the YAxisFeatureData's to TRANS_DISP type - I'm assuming that that's linear displacement? And I changed the first FeatureData component to 0 (x?) and the second FeatureData component to 1 (y?)
' Set the type of plots
nameYAxis(0) = "swMotionPlotAxisType_TRANS_DISP"
swSimPlotYAxisFeatureData(0).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_DISP 'swMotionPlotAxisType_TRANS_DISP
swSimPlotYAxisFeatureData(0).Component = 0
nameYAxis(1) = "swMotionPlotAxisType_TRANS_DISP"
swSimPlotYAxisFeatureData(1).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_DISP
swSimPlotYAxisFeatureData(1).Component = 1
This is where it falls off the rails. I changed the swFace type to Object because I couldn't figure out what it should be. I've recorded many a macro and based on the SelectByID2 returns I've gotten DATUMPOINT, SKETCHPOINT, EXTSKETCHPOINT - none of which are declaration types.
' Get the entity whose motion you want analyzed
swFace(0) = swSelMgr.GetSelectedObject6(1, -1)
swFaceArray = swFace
swSimPlotYAxisFeatureData(0).Entities = swFaceArray
swSimPlotYAxisFeatureData(1).Entities = swFaceArray
' Get the plot's x-axis and y-axes values
swYAxisArray = swSimPlotYAxisFeatureData
PlotOutput = MotionStudyResults.GetValues(swSimPlotFeatureData, swSimPlotXAxisFeatureData, (swYAxisArray))
When I run the code, swFace(0), swFaceArray, swSimPlotYAxisFeatureData(x), and swYAxisArray all have some type of value, but PlotOutput does not.
I'm not quite understanding swSelMgr.GetSelectedObject6(1,-1). In the SelectByID2 calls above, two items are selected.
In GetSelectedObject6(index, mark), if index = 1 then are you just returning the first item selected? But doesn't the displacement plot require two objects?
I took out the output to the Immediate Window a swPlotFeature kept crashing (= Nothing).
If I can get this code to work, I'm pretty sure I can turn it into a loop to iterate through the 144 points. However, it would be great to combine the x & y displacement plots into one plot.
Please peruse the above and let me know where I'm going wrong.
Thank you.