AnsweredAssumed Answered

Replace model on all views and all sheets of the drawing

Question asked by Andrei Selivanau on Feb 27, 2020
Latest reply on Feb 27, 2020 by Keith Rice

Hi all,

maybe you could help me on this...

I have a code which suppose to do next: 

I select the part "Z" in the assembly. Then opens file dialog where I chose old drawing(created for previous revision of this part, let's call it "Y") where I want to replace all views to refer "Z" instead "Y".

I have a drawing open. I would like to not select or activate anything, just iterate through all views and replace model on all views. ReplaceViewModel method somehow returns always false. One more thing: what are Instances in this method? How to get them?


current code


Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel.GetType = swDocASSEMBLY Or swModel.GetType = swDocPART Then
Set swSelMgr = swModel.SelectionManager
sSelCount = swSelMgr.GetSelectedObjectCount()
ReDim aComponent(sSelCount - 1)
Filter = "SOLIDWORKS Drawings (*.slddrw)|*.slddrw|All Files (*.*)|*.*|"

If sSelCount <> 0 Then sUbound = sSelCount - 1 Else sUbound = sSelCount
For i = 0 To sUbound
If sSelCount <> 0 Then
Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(i + 1, 0)
swPath = swSelComp.GetPathName
swPath = swModel.GetPathName
End If

'manipulating partnumber

sBaseFilePath = Left(swPath, InStrRev(swPath, ".") - 1)
aFileNamePartNo = Split(sBaseFilePath, "\")
sFileNamePartNo = aFileNamePartNo(UBound(aFileNamePartNo))
sFileNamePartNo2 = Split(sFileNamePartNo, " ")(0)
sDrawingForReusePath = swApp.GetOpenFileName("Select old drawing you want to reuse with " & sFileNamePartNo2, swPath, Filter, fileOptions, fileConfig, fileDispName)


'open drawing
Set swDrawing = swApp.OpenDoc6(sDrawingForReusePath, swDocDRAWING, swOpenDocOptions_Silent, "", swErrors, swWarnings)
Set swDrawing = swApp.ActiveDoc
Set swModelDocExt = swDrawing.Extension
Set swView = swDrawing.GetFirstView
ReDim aViews(0)
ReDim aViewName(0)
'For j = 1 To swDrawing.GetSheetCount
'Set swView = swView.GetNextView
'For k = 1 To UBound(aViews)
Do While Not swView Is Nothing
k = k + 1
ReDim Preserve aViews(k)


Set swView = swView.GetNextView
Set aViews(k) = swView
If Not swView Is Nothing Then
ReDim Preserve aViewName(k)
aViewName(k) = aViews(k).Name
End If
'Next k
'Next j


here it doesnt work:
boolstatus = swDrawingComponent.ReplaceViewModel(sBaseFilePath & ".sldprt", (aViews), (aInstances))
boolstatus = swDrawing.Rebuild(1)
longstatus = swModelDocExt.SaveAs(sBaseFilePath & ".slddrw", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, swErrors, swWarnings)


End If
End Sub



Any help, or examples maybe?