SolidWorks*Excel+VBA-批量工程图
- 2026-11-10 04:06:20
SolidWorks*Excel+VBA-批量工程图工程图还得是批量生成来的爽 上内核 需要设置下模板,我是先存表格里了,当然,程序里直接写死也是ok的。 
SolidWorks主程序系统设置里的文件位置加不加无所谓,反正VBA不受这个限制,加一下么方便后续反复修正模板。 

已关注
关注
重播 分享 赞
Sub NewSwDrw() '生成工程图Dim swApp As SldWorks.SldWorks, FullModelPath As String, DrwTemplate As StringDim OpennedModel As ModelDoc2, PartModel As PartDoc, SwErr As Long, PropVal As StringDim k As Integer, posn As Long, SwDrw As DrawingDocDim Results As String, Suffix(), ModelType(), OpenVal As LongOn Error Resume NextSet swApp = GetObject(, "SldWorks.Application")If swApp Is Nothing ThenMsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人"Exit SubEnd IfSuffix = Array(".SLDPRT", ".SLDASM"): ModelType = Array(swDocPART, swDocASSEMBLY)DrwTemplate = ThisWorkbook.Sheets("SW0 参数设置").Range("D3").Value & "\" & _ThisWorkbook.Sheets("SW0 参数设置").Range("C3").ValueWith ThisWorkbook.ActiveSheetIf myselcondi ThenswApp.CloseAllDocuments (True)For k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在生成工程图:", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)If Dir(.Cells(posn, 2) & "\" & .Cells(posn, 3) & ".SLDDRW") <> "" ThenResults = "已有同名工程图!"ElseFullModelPath = .Cells(posn, 2) & "\" & .Cells(posn, 3) & .Cells(posn, 4)OpenVal = ModelType(WorksheetFunction.Match(UCase(.Cells(posn, 4)), Suffix, 0) - 1)Set OpennedModel = swApp.OpenDoc2(FullModelPath, OpenVal, False, False, True, SwErr)If OpennedModel Is Nothing ThenResults = "模型打开失败!"ElseSet SwDrw = swApp.NewDocument(DrwTemplate, 0, 0, 0)If SwDrw Is Nothing ThenResults = "工程图生成失败!"ElseSwDrw.InsertModelInPredefinedView FullModelPathSwDrw.SaveAs2 .Cells(posn, 2) & "\" & .Cells(posn, 3) & ".SLDDRW", swSaveAsCurrentVersion, False, TrueswApp.QuitDoc .Cells(posn, 2) & "\" & .Cells(posn, 3) & ".SLDDRW"Set SwDrw = NothingResults = "已生成工程图!"End IfswApp.QuitDoc FullModelPath: Set OpennedModel = NothingEnd IfEnd If.Cells(posn, 1) = Results: Results = ""NextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithErr.ClearSet swApp = NothingApplication.StatusBar = FalseEnd Sub



本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。