Hi,
I am trying to move component along X axis with macro. I've foundthis sample in API tutorial, but i cant manage to transform it tomake a translation from one point to another. Can someone pleasehelp me. Ohh and i need to keep all mates active.
' Problem:
' This code shows how to use the MathUtility object
' to directly create a transformation matrix (object)
' that represents rotation about a point and an axis,
' without having to know details of the OpenGL transformations.
'
' Preconditions:
' (1) Assembly is open and fully resolved.
' (2) Assembly component is selected.
'
' Postconditions: Selected component is rotated 90° aboutassembly X axis.
'
'------------------------------------------------------------------
Option Explicit
Const PI As Double = 3.14159
Const RadPerDeg As Double = PI / 180#
' DragOperator::TransformType
' Translation 0
' Transform is translation only
'
' Axial rotation 1
' Transform is rotation only
'
' General 2
' Transform can be translation or rotation or both
' DragOperator:
ragMode
' Minimum Move 0
' Move smallest number of geometries
'
' Maximum Move 1
' Move geometries rigidly if possible
'
' Relaxation 2
' Solve geometries using relaxation
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swDragOp As SldWorks.DragOperator
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swXform As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim swOriginPt As SldWorks.MathPoint
Dim swX_Axis As SldWorks.MathVector
Dim nPts(2) As Double
Dim vData As Variant
Dim nNow As Single
Dim i As Long
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swDragOp = swAssy.GetDragOperator
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set swMathUtil = swApp.GetMathUtility
nPts(0) = 0#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swOriginPt = swMathUtil.CreatePoint(vData)
nPts(0) = 1#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swX_Axis = swMathUtil.CreateVector(vData)
' This is an incremental rotation,
' so angle is always the same
Set swXform = swMathUtil.CreateTransformRotateAxis( _
swOriginPt, swX_Axis, 1# * RadPerDeg)
bRet = swDragOp.AddComponent(swComp, False)
Debug.Assert bRet
swDragOp.CollisionDetectionEnabled = False
swDragOp.DynamicClearanceEnabled = False
' Axial rotation
swDragOp.TransformType = 1
' Solve by relaxation
swDragOp.DragMode = 2
bRet = swDragOp.BeginDrag
Debug.Assert bRet
For i = 0 To 90
' Returns false if drag fails, for example, because of a collision
bRet = swDragOp.Drag(swXform)
' Wait for 0.1 secs
nNow = Timer
While Timer < nNow + 0.1
' Process event loop
DoEvents
Wend
Next i
bRet = swDragOp.EndDrag
Debug.Assert bRet
End Sub
SolidworksApi macros
I am trying to move component along X axis with macro. I've foundthis sample in API tutorial, but i cant manage to transform it tomake a translation from one point to another. Can someone pleasehelp me. Ohh and i need to keep all mates active.
' Problem:
' This code shows how to use the MathUtility object
' to directly create a transformation matrix (object)
' that represents rotation about a point and an axis,
' without having to know details of the OpenGL transformations.
'
' Preconditions:
' (1) Assembly is open and fully resolved.
' (2) Assembly component is selected.
'
' Postconditions: Selected component is rotated 90° aboutassembly X axis.
'
'------------------------------------------------------------------
Option Explicit
Const PI As Double = 3.14159
Const RadPerDeg As Double = PI / 180#
' DragOperator::TransformType
' Translation 0
' Transform is translation only
'
' Axial rotation 1
' Transform is rotation only
'
' General 2
' Transform can be translation or rotation or both
' DragOperator:
' Minimum Move 0
' Move smallest number of geometries
'
' Maximum Move 1
' Move geometries rigidly if possible
'
' Relaxation 2
' Solve geometries using relaxation
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swDragOp As SldWorks.DragOperator
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swXform As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim swOriginPt As SldWorks.MathPoint
Dim swX_Axis As SldWorks.MathVector
Dim nPts(2) As Double
Dim vData As Variant
Dim nNow As Single
Dim i As Long
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swDragOp = swAssy.GetDragOperator
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set swMathUtil = swApp.GetMathUtility
nPts(0) = 0#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swOriginPt = swMathUtil.CreatePoint(vData)
nPts(0) = 1#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swX_Axis = swMathUtil.CreateVector(vData)
' This is an incremental rotation,
' so angle is always the same
Set swXform = swMathUtil.CreateTransformRotateAxis( _
swOriginPt, swX_Axis, 1# * RadPerDeg)
bRet = swDragOp.AddComponent(swComp, False)
Debug.Assert bRet
swDragOp.CollisionDetectionEnabled = False
swDragOp.DynamicClearanceEnabled = False
' Axial rotation
swDragOp.TransformType = 1
' Solve by relaxation
swDragOp.DragMode = 2
bRet = swDragOp.BeginDrag
Debug.Assert bRet
For i = 0 To 90
' Returns false if drag fails, for example, because of a collision
bRet = swDragOp.Drag(swXform)
' Wait for 0.1 secs
nNow = Timer
While Timer < nNow + 0.1
' Process event loop
DoEvents
Wend
Next i
bRet = swDragOp.EndDrag
Debug.Assert bRet
End Sub
SolidworksApi macros

Dan Miel
If you need the mates to stay active, use a distance mate along theX axis.
Use your macro to update the value. No transforms needed.
Scott
I realized that this isnt a good way to do it, i dont even know ifi could do this with this code.
Becouse in this code i'm rotating axis, but i would like to dotranslation on axis. I couldnt understand whats the diffrence inthis code right here:
' DragOperator::TransformType
' Translation 0
' Transform is translation only
' Axial rotation 1
' Transform is rotation only
' General 2
' Transform can be translation or rotation or both
swDragOp.TransformType = 1 ' 1 is for rotation 0 is for translation
But i dont see any diffrance between rotation and translation...
After that unsuccessfull try i used this code
'rotation
aux_a(0) = 1#: aux_a(1) = 0#: aux_a(2) = 0#
aux_a(3) = 0#: aux_a(4) = 1#: aux_a(5) = 0#
aux_a(6) = 0#: aux_a(7) = 0#: aux_a(8) = 1#
' Translation atention: a(9)=x axis a(10)=z axis a(11)=y axis !!!
aux_a(9) = x(i): 'x os
aux_a(10) = y(i): 'y os
aux_a(11) = z(i): 'z os
' Scale
aux_a(12) = 1#
' PERSPECTIV
aux_a(13) = 0#: aux_a(14) = 0#: aux_a(15) = 0#
'---------------------------------------------------------------
vXform = aux_a
insted of this:
nPts(0) = 0#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swOriginPt = swMathUtil.CreatePoint(vData)
nPts(0) = 1#
nPts(1) = 0#
nPts(2) = 0#
vData = nPts
Set swX_Axis = swMathUtil.CreateVector(vData)
' This is an incremental rotation,
' so angle is always the same
Set swXform = swMathUtil.CreateTransformRotateAxis(swOriginPt,swX_Axis, 1# * RadPerDeg)
Can i ask if this line works like interfence detection? If i use itit doesnt stop my moving (bRet is always true), even though it is acollision(interference) between two parts.
swDragOp.CollisionDetectionEnabled = True
bRet = swDragOp.BeginDrag
Debug.Assert bRet
For i = 0 To 90
' Returns false if drag fails, for example, because of a collision
bRet = swDragOp.Drag(swXform)
That is why i used interference detection, but i have like 3000points so this is a very slow method.
Hope you get an idea what i am trying to do and someone can help.
Thanks
component, that will move has to be selected, assembly moves afterthe component!
x,y,z are translation coordinates
Function Translate(x, y, z, fileLenght,)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swDragOp As SldWorks.DragOperator
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swXform As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim Part As Object
Dim vData As Variant
Dim nNow As Single
Dim bRet As Boolean
Dim aux_a(15) As Double
Dim x1(3), y1(3), z1(3) As Double
Dim xF, yF, zF As Double
Dim dX, dY, dZ As Double
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swDragOp = swAssy.GetDragOperator
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set swMathUtil = swApp.GetMathUtility
Set Part = swApp.ActiveDoc
For j = 0 To fileLenght - 1
If (j <> 0) Then
dX = x(j) - x(j - 1)
dY = y(j) - y(j - 1)
dZ = z(j) - z(j - 1)
End If
If (j = (fileLenght - 1)) Then
dX = 0
dY = 0
dZ = 0
End If
dX = dX / 1000
dY = dY / 1000
dZ = dZ / 1000
'-------------------------MATRIZ------------------------------
'rotation
aux_a(0) = 1#: aux_a(1) = 0#: aux_a(2) = 0#
aux_a(3) = 0#: aux_a(4) = 1#: aux_a(5) = 0#
aux_a(6) = 0#: aux_a(7) = 0#: aux_a(8) = 1#
' Translation atention: a(9)=x axis a(10)=y axis a(11)=z axis !!!
aux_a(9) = dX / 10 'x(j) / 10
aux_a(10) = dY / 10 'y(j) / 10
aux_a(11) = dZ / 10 'z(j) / 10
' Scale
aux_a(12) = 1#
' PERSPECTIV
aux_a(13) = 0#: aux_a(14) = 0#: aux_a(15) = 0#
'---------------------------------------------------------------
vXform = aux_a
Set swXform = swMathUtil.CreateTransform((vXform))
bRet = swDragOp.AddComponent(swComp, False)
swDragOp.CollisionDetectionEnabled = False
swDragOp.DynamicClearanceEnabled = False
'translation rotation
swDragOp.TransformType = 0
'Solve by relaxation
swDragOp.DragMode = 2
bRet = swDragOp.BeginDrag
If (x(j) = 0 And y(j) = 0 And z(j) = 0) Then
n = 1
Else
n = 9
End If
If (j = 2) Then
End If
For i = 0 To n 'Makes the translation operation (this case 1unit)10 TIMES
bRet = swDragOp.Drag(swXform)
swModel.GraphicsRedraw2
nNow = Timer
While Timer < nNow + 0.1
' Process event loop
DoEvents
Wend
swModel.GraphicsRedraw2
Next i
bRet = swDragOp.EndDrag
Next j
Part.EditRebuild3
End Function