本帖最后由 idonot 于 2019-5-22 23:29 编辑
试了好几次,也没有实现闷大的功能,仔细查看了代码以后,感觉双引号内少了\,擅自修改发布代码,如有失礼之处,请多包涵。只为帮助更多人...
- Sub main()
- Set swApp = Application.SldWorks
- Set Model = swApp.ActiveDoc
- If Model Is Nothing Then Exit Sub
- ModelPathName = Model.GetPathName
- ModelConfigName = swApp.GetActiveConfigurationName(ModelPathName) '當前模型的當前配置名稱
- ModelPath = Left(ModelPathName, InStrRev(ModelPathName, ""))
- ModelName = Right(ModelPathName, Len(ModelPathName) - Len(ModelPath))
- DrawingFileName = Dir(ModelPath & "*.slddrw") '獲取首個工程圖檔案名稱
- NoDrawingFound = True
- Do Until DrawingFileName = "" '直至獲取到空值
- traverse = False 'True
- Search = False
- addreadonlyinfo = False
- depends = swApp.GetDocumentDependencies2(ModelPath & DrawingFileName, traverse, Search, addreadonlyinfo) '工程圖含有的全部模型檔案名稱
- WithModel = False
- If Not IsEmpty(depends) Then
- idx = 1
- While idx <= UBound(depends)
- If ModelPathName = depends(idx) Then WithModel = True '工程圖是否含有當前模型檔案名稱
- idx = idx + 2
- Wend
- End If
- If WithModel Then '是否含有當前模型檔案名稱
- Set Drawing = swApp.OpenDoc(ModelPath & DrawingFileName, 3) '開啟工程圖
- Dim longstatus As Long
- swApp.ActivateDoc2 DrawingFileName, False, longstatus '顯示工程圖
- myViewss = Drawing.GetViews '所有視圖
- ModelConfigInDrawing = False
- For i = 0 To UBound(myViewss) '每頁
- myViews = myViewss(i)
- SheetName = myViews(0).Name '每頁圖頁名稱
- ModelInSheet = False
- For j = 0 To UBound(myViews)
- If ModelPathName = myViews(j).GetReferencedModelName And ModelConfigName = myViews(j).ReferencedConfiguration Then '模型檔名及配置名稱都吻合
- ModelInSheet = True
- ModelConfigInDrawing = True
- End If
- Next
- If ModelInSheet Then Drawing.ActivateSheet SheetName '跳到含有當前模型及配置的圖頁
- Next
-
- If Not ModelConfigInDrawing Then '開啟了的工程圖不吻合所有條件
-
-
- MsgBox "此工程圖雖然含有 " & ModelName & Chr(10) & "但沒有對應的配置 " & ModelConfigName '如覺得此提示信息有阻礙, 可整句刪除
-
- swApp.ActivateDoc2 ModelPathName, False, longstatus '顯示本來的模型 (雖然開啟了的工程圖不吻合條件, 但必須保持開啟, 以免影響其他當前工作)
-
- End If
-
- NoDrawingFound = False
-
- End If
-
- DrawingFileName = Dir '獲取下一個工程圖檔案名稱
-
- Loop '循環
- If NoDrawingFound Then MsgBox "在文件夹 " & ModelPath & Chr(10) & "找不到含有 " & ModelName & " 的工程图" '如覺得此提示信息有阻礙, 可整句刪除
- End Sub
复制代码 可能是论坛系统问题,我上传的代码里面"\"也消失不见了。具体是代码的第7行的双引号内添加\即可。
|