SolidWorks專門論壇 SolidWorks forum

 找回密碼
 註冊
查看: 14479|回復: 7

API 組合件 移動零組件的方式

[複製鏈接]
發表於 2015/1/10 11:31:28 | 顯示全部樓層 |閱讀模式
想請教各位先進 SolidWorks 於組合件中,錄製移動零組件只能錄製到打開此功能,卻無法使用裡面的功能,例如碰撞停止或是透過(X,Y,Z)座標移動零件。

想請問各位有辦法解決這個問題嗎??感激大家...
發表於 2015/1/11 19:38:57 | 顯示全部樓層
沒寫過這方面的 API
剛剛查了一下, 你可以試試 IDragOperator Interface
若要用錄製的方式寫出移動零組件的 API....應該是不行的
錄製的 API 會用座標的方式去選擇零組件, 問題是你只要隨便移動, 放大縮小零組件, 座標便不一樣
你可以用單步除錯去看問題出在哪
應該是沒選到零組件............
 樓主| 發表於 2015/1/12 18:38:06 | 顯示全部樓層
謝謝...本身於組合件中有選項為移動零組件或旋轉零組件...想針對選定的零組件進行平移...
但是錄製出來的API只有 ssemblyDoc.TranslateComponent() 這只有開啟功能...但無法使用內部功能例如碰撞停止...
發表於 2015/1/12 21:35:47 | 顯示全部樓層
SolidWorks API Help
===============================
TranslateComponent Method (IAssemblyDoc)
Displays the Move Component PropertyManager page.

TranslateComponent 的確只能顯示出對話框.....

錄製巨集只能用來參考, 並不會產生一段可重複使用的程式碼
所以程式還是得自己寫.........

如果要寫, 可以試試我說的 IDragOperator Interface
DragOperator.AddComponent 可以設定要移動的 Components
DragOperator.Drag 可以設定 Translate matrix
再用 DragOperator.BeginDrag 執行移動

 樓主| 發表於 2015/1/14 18:28:44 | 顯示全部樓層
OK...感謝...我馬上去試試...
 樓主| 發表於 2015/1/14 19:08:34 | 顯示全部樓層
您好...我剛剛試了一下...

我這樣寫

Dim Value As Compoent2

Value = swAssembly.AddCompoent2("工件-1",0,0,0.1)

這樣會出錯??

想請問謝謝
發表於 2015/1/19 16:37:30 | 顯示全部樓層

VBA移動零組件

VBA移動零組件

'以下VBA
Dim swApp As Object
Dim swAssy As Object
Dim swDragOp As Object
Dim swSelMgr As Object
Dim swComp As Object
Dim swXform As Object
Dim aux_a(15) As Double
Dim vData As Variant
Dim i As Long
Dim bRet As Boolean

Sub main()
'*先決條件,必需先選取要移動零件的面
Set swApp = Application.SldWorks
Set swModel = swApp.ActivateDoc("AA1") '組合圖的檔案名稱
Set swAssy = swModel
Set swDragOp = swAssy.GetDragOperator
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set swMathUtil = swApp.GetMathUtility
        aux_a(0) = 0#: aux_a(1) = 0#: aux_a(2) = 0#
        aux_a(3) = 0#: aux_a(4) = 0#: aux_a(5) = 0#
        aux_a(6) = 0#: aux_a(7) = 0#: aux_a(8) = 0#
        aux_a(9) = 0.001 'x軸向移動1mm
        aux_a(10) = 0#
        aux_a(11) = 0#
        aux_a(12) = 0#
        aux_a(13) = 0#: aux_a(14) = 0#: aux_a(15) = 0#
vData = aux_a
Set swXform = swMathUtil.CreateTransform((vData))
bRet = swDragOp.AddComponent(swComp, False)
MsgBox (bRet) '通知你有沒有選取要移動的面
swDragOp.CollisionDetectionEnabled = True
bRet = swDragOp.CollisionDetection(swComp, True, True) '第三個參數為碰撞時停止
swDragOp.DynamicClearanceEnabled = False
swDragOp.TransformType = 0
swDragOp.DragMode = 2
bRet = swDragOp.BeginDrag
        For i = 0 To 204 '204是我的零件在X軸移動200就會碰撞
            bRet = swDragOp.Drag(swXform)
            '~下面只是讓他跑慢點才看的到怎麼動而以~
            nNow = Timer
            While Timer < nNow + 0.01
            DoEvents
            Wend
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Next i
        bRet = swDragOp.EndDrag
End Sub


 樓主| 發表於 2015/2/5 19:27:58 | 顯示全部樓層
最近都沒空...一上來就看到大大的回覆...非常感激...晚點來試看看...
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版上論壇|論壇來自幾何科技 論壇架構版次 20240312

GMT+8, 2024/4/27 00:10 , Processed in 0.253402 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表