好了,我从来没有得到的阵列的载体部工作,并最终使用变换,API的帮助是不是非常清楚,当涉及到使用身体与功能的组件,但它似乎工作相同。
(一些这可能看起来不洁净,但这里的格式是给我找麻烦,所以我不得不四处洗牌一些事情。)
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
来源
2017-04-20 13:56:45
CZK
可能你已经尝试过的代码可以帮助我们得到你可能有用的东西 – AndrewK