Solidworks二次開發-05-裝配體中插入零部件
2024-07-21 02:16:14
供稿:網友
 
solidworks二次開發--裝配體中插入零部件 
 
 
在往裝配體中插入零部件時,我們使用addcomponent 函數。如果需要選定零部件的配置,則需要使用addcomponent4。
先學習下語法:
 
addcomponent4:
retval = assemblydoc.addcomponent4 ( compname, configname, x, y, z)
 
input:         (bstr) compname                    path name of a loaded part or assembly to add as a component 
input:         (bstr) configname                   name of the configuration from which to load the component
input:        (double) x                                      x coordinate of the component center
input:         (double) y                                      y coordinate of the component center
input:        (double) z                                      z coordinate of the component center
output:     (lpcomponent2) retval          pointer to the component2 object
 
需要注意的是:參數1為文件的全名(包括路徑);參數2為文件的配置名稱;當函數執行成功購返回一個指向該零件的指針。
于是我們可以如下寫一個小程序,用來給裝配體中插零件:
‘filename:insertpart.swp
‘write by 
arden
 2005-4-4
‘this function add a part called “零件1.sldprt” in currentworkingdirectory
‘precondition is there has a part document called  “零件1.sldprt” in currentworkingdirectory
‘and it has a configuration called “配置1”
 
dim swapp as sldworks.sldworks
dim model as modeldoc2
dim pth as string
dim strpath as string
 
sub insertpart()
set swapp = application.sldworks
strpath = swapp.getcurrentworkingdirectory  ‘當前工作路徑
set model = swapp.activedoc
pth = strpath & "零件1.sldprt"             ‘得到文件的fullpath全名
model.addcomponent4 pth, "配置1", 0, 0, 0   ‘添加零部件
end sub
 
然而,這個程序比不是想象中那么好用。為什么呢??回頭看addcomponent4的remark,上面這樣寫:
the specified file must be loaded in memory. a file is loaded into memory when you load the file in your 
solidworks session (sldworks::opendoc6) or open an assembly that already contains the file.
就是說你想指定的插入的文件必須在調用函數之前已經在內存中加載了。
不習慣,你就不能直接打開多簡單,沒辦法,我還沒有找到好的方法,只能按人家的來:
看看下面的函數opendoc6,它打開一個文檔:
 
opendoc6:
retval = sldworks.opendoc6 ( filename, type, options, configuration, &errors, &warnings )
input:        (bstr) filename                       document name or full path if not in current directory, including extension
input:        (long) type                                    document type as defined in swdocumenttypes_e
input:        (long) options                              mode in which to open the document as defined in swopendocoptions_e
input:        (bstr) configuration                 model configuration in which to open this document:
applies to parts and assemblies, not drawings
if this argument is empty or the specified configuration is not present in the model, 
the model is opened in the last-used configuration.
output:     (long) errors                                 load errors as defined in swfileloaderror_e
output:     (long) warnings                           warnings or extra information generated during the open operation as defined in swfileloadwarning_e
return:    (lpdispatch) retval                  pointer to a dispatch object, the newly loaded modeldoc2, or null if failed to open
 
這個函數參數1就是文檔的全名,參數2是要插入的類型描述,其中0123分別表示: 
0       swdocnone:不是sw文件
1       swdocpart:零件
2       swdocassembly:裝配體
3       swdocdrawing:工程圖
如果想使用swdocnone,需要定義:
public enum swdocumenttypes_e
}-->    }-->swdocnone = 0
}-->    }-->swdocpart= 1
}-->    }-->swdocassembly = 2
    swdocdrawing=3
end enum
參數3是打開文檔的模式,一般我們就選擇swopendocoptions_silent  用0 表示,當然還有只讀、只看等選項
參數4是打開選項,一般置空
后面是兩個output,用來顯示錯誤打開時的提示
函數返回一個指向打開文件的指針。
 
按照上面的要求我們在向裝配體中插入一個零部件時,需要這樣步驟:
1、得到裝配體
2、使用opendoc6打開需要插入的零件
3、使用addcomponent4插入零件到裝配體
我們上面的程序需要這樣來修改一下,添加了一個打開文檔的函數:
 
' ******************************************************************************
' insertpart 03/21/05 by 
arden
'插入零件1
'前提條件:在裝配體所在文件夾中有零件“零件1”存在,并且零件1有配置“配置1”
' ******************************************************************************
dim swapp as sldworks.sldworks
dim model as modeldoc2
dim ysbmodel as modeldoc2
dim pth as string
dim strpath as string
dim nerrors as long
dim nwarnings as long
 
 
sub insertpart()
set swapp = application.sldworks
strpath = swapp.getcurrentworkingdirectory
set model = swapp.activedoc
pth = strpath & "零件1.sldprt"
openysb (pth)  ‘在添加零部件之前,先打開它
model.addcomponent4 pth, "配置1", 0, 0, 0
end sub
 
'這個函數打開零件1
sub openpart(byval pth as string)
dim path as string
dim newswapp as sldworks.sldworks
set newswapp = application.sldworks
 
path = pth
set ysbmodel = newswapp.opendoc6(path, 1, swopendocoptions_silent, "", nerrors, nwarnings)
ysbmodel.visible = false  ‘我不想看到零件1
end sub
 
這樣的做法我感覺比較笨~為了趕工程進度我沒有再去尋找好的方法,如果您知道有好的方法請告知我,萬分感謝。