CreateBodyFromBox3 boxAxis向量问题

时间:2017-04-18 17:23:02

标签: api macros solidworks

我正在使用CreateBodyFromBox3为宏特征创建一组实体,并且需要将其放置在围绕z轴的角度。我可以设置挤出矢量,使其与X或Y成一个角度,没有任何问题,但如果它与Z相关,则与挤出检测垂直的矢量显然具有Z分量。形状是正确的,所以我知道数组中的所有其他值都是正确的,并且创建的主体没有任何错误。

任何解决方法的想法?我试图使用变换,并且我能够将变换数组数据变为Variant,但是当我尝试使变换数组数据等于Variant时,它只是将Variant重置为原始数组数据。

(显示的坐标系与模型空间坐标系相同)

Rotated about X dblData(3) = 0 dblData(4) = 0.5 dblData(5) = -0.866

Rotated about Z dblData(3) = 0.5 dblData(4) = -0.866 dblData(5) = 0

1 个答案:

答案 0 :(得分:1)

好的,所以我从来没有让数组的矢量部分工作,只是最终使用变换,API帮助在使用on body和功能组件时并不十分清楚但似乎同样的工作。

(其中一些看起来可能看起来不太合适,但这里的格式化给我带来了麻烦所以我不得不改变一些东西。)

Public Sub CreateBody(bInsert As Boolean)
Dim swWorkBody As SldWorks.Body2, swToolBody1 As SldWorks.Body2
Dim swToolBody2 As SldWorks.Body2
Dim swTempBody_1 As SldWorks.Body2, swTempBody_2 As SldWorks.Body2
Dim swModeler As SldWorks.Modeler
Dim swFaultEnt As SldWorks.FaultEntity
Dim swTransform As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility, vTransform As Variant
Dim swMoveBody As SldWorks.MoveCopyBodyFeatureData

Dim dblData(8) As Double
Dim vBody As Variant
Dim lngErr As Long
Dim Y As Double, theta As Double, pi As Double
Dim bRet As Boolean
Dim bReRet As Boolean

On Error GoTo errH
If bInsert = True Then GetMetric 
pi = 4 * Atn(1)
theta = Atn(mRoofSlope / (12 * m)) ' * (pi / 180)
Y = (mWidth - (2 * (mExtWallDepth + mIntWallDepth))) * Tan(theta)

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModeler = swApp.GetModeler
Set swMathUtil = swApp.GetMathUtility

dblData(0) = 0  'X Center
dblData(1) = 0  'Y Center
dblData(2) = 0  'Z Center
dblData(3) = 0  'X Direction Vector
dblData(4) = 1  'Y Direction Vector
dblData(5) = 0  'Z Direction Vector
dblData(8) = mHight + Y  'Hight of Extrution along Y

'Hollow Out Ext Walls
    'Make Tool Body
        dblData(6) = mWidth - (2 * (mExtWallDepth + mIntWallDepth))   'Width of Extrution along X
        dblData(7) = mLength - (2 * (mExtWallDepth + mIntWallDepth))  'Length of Extrution along Z

        Set swToolBody1 = swModeler.CreateBodyFromBox3(dblData)
        Set swFaultEnt = swToolBody1.Check3
        ProcessFaultEntity swApp, swModel, swFaultEnt

    'Make Work Pice Body
        dblData(6) = mWidth   'Width of Extrution along X
        dblData(7) = mLength  'Length of Extrution along Z

        Set swWorkBody = swModeler.CreateBodyFromBox3(dblData)
        Set swFaultEnt = swWorkBody.Check3
        ProcessFaultEntity swApp, swModel, swFaultEnt

        vBody = swWorkBody.Operations2(SWBODYCUT, swToolBody1, lngErr)
        Set swTempBody_1 = vBody(0)

'Create Low Side Walls
    'Make Tool Body
        dblData(0) = (mWidth / 2) - ((mExtWallDepth + mIntWallDepth) / 2)   'X Center
        dblData(1) = dblData(8) - Y                                         'Y Center
        dblData(6) = mExtWallDepth + mIntWallDepth                          'Width of Extrution along X
        dblData(8) = Y                                                      'Hight of Extrution along Y

        Set swToolBody1 = swModeler.CreateBodyFromBox3(dblData)
        Set swFaultEnt = swToolBody1.Check3
        ProcessFaultEntity swApp, swModel, swFaultEnt

        vBody = swTempBody_1.Operations2(SWBODYCUT, swToolBody1, lngErr)
        Set swTempBody_2 = vBody(0)

    'Create B&D Wall Slop
        dblData(0) = 0  'X Center
        dblData(1) = 0  'Y Center
        dblData(2) = 0  'Z Center
    '=============== This is were I was trying to use the vector method ==========
    '=============== I was just using 30 degrees so i could see it ===============
        dblData(3) = 0.5
        dblData(4) = -0.866
        dblData(5) = 0
    '========================================================================
        dblData(6) = mWidth - 2 * (mExtWallDepth + mIntWallDepth) + (Y * Tan(theta)) / 2 'Width of Extrution along X
        dblData(7) = mLength     'Length of Extrution along Z

        Set swToolBody2 = swModeler.CreateBodyFromBox3(dblData)
        Set swFaultEnt = swToolBody2.Check3
        ProcessFaultEntity swApp, swModel, swFaultEnt

  '======================== This is the fix ========================
        bRet = swToolBody2.GetCoincidenceTransform2(swToolBody2, swTransform)
        vTransform = swTransform.ArrayData

        vTransform(0) = Cos(theta)
        vTransform(1) = -Sin(theta)
        vTransform(2) = 0
        vTransform(3) = Sin(theta)
        vTransform(4) = Cos(theta)
        vTransform(5) = 0
        vTransform(6) = 0
        vTransform(7) = 0
        vTransform(8) = 1
        vTransform(9) = 0
        vTransform(10) = (mHight + Y / 2)
        vTransform(11) = 0

        swTransform.ArrayData = vTransform
        vTransform = swTransform.ArrayData
        bReRet = swToolBody2.ApplyTransform(swTransform)
        vBody = swTempBody_2.Operations2(SWBODYCUT, swToolBody2, lngErr)
'========================================================================    
'Set to Macro Body
Set swHouseBody = vBody(0)   'Set to Final Body

If STATE <> 0 Then swHouseBody.Display3 swModel, 255, 0
swModel.ViewZoomtofit

Exit Sub
errH:
Debug.Print "lngErr: " & lngErr
Debug.Print "Err Number: " & Err.Number
Debug.Print "Err Description: " & Err.Description
Err.Clear
Set swFaultEnt = Nothing
Set swWorkBody_1 = Nothing
Set swWorkBody_2 = Nothing
Set swToolBody1 = Nothing
Set swToolBody2 = Nothing
Set swHouseBody = Nothing
End Sub

Private Sub ProcessFaultEntity(swApp As SldWorks.SldWorks, swModel As             SldWorks.ModelDoc2, swFaultEnt As SldWorks.FaultEntity)
Dim nCount As Long
Dim swEnt As SldWorks.Entity
Dim bRet As Boolean
Dim i  As Long
nCount = swFaultEnt.Count: If 0 = nCount Then Exit Sub 'Else print the error code for each fault
For i = 0 To nCount - 1
    Set swEnt = swFaultEnt.Entity(i)
    If Not swEnt Is Nothing Then
        bRet = swEnt.Select4(True, Nothing): Debug.Assert bRet
    End If
    Debug.Print "    Fault[" & i & "] = " & swFaultEnt.ErrorCode(i)
Next i
End Sub