loading and activating a model from VB
loading and activating a model from VB
(OP)
This is what I am trying to do:
1) Open a drawing in SW.
2) Run a macro/program
3) The macro will determine the solid model used to create the drawing, will load and activate the model, will read the
custom properties from the model, will reactivate the drawing and will write the custom properties into the drawing.
I have difficulties in opening the model. The code I wrote looks like this:
Dim swApp As Object
Dim Part As Object
Dim retval As Boolean
Dim view0 As Object
Dim view1 As Object
Dim Part1 As Object
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
'Form1.Height = 4400
If Part Is Nothing Then
msg = "A SolidWorks document needs to be loaded!"
Style = vbExclamation ' Error style dialog
Titlemsg = "Model Properties" ' Define title
Call MsgBox(msg, Style, Titlemsg) ' Display error message
returnOK = False
swApp.Visible = True
End ' If no model currently loaded, then exit
Else
docType = Part.GetType
End If
.....................
.....................
.....................
If (docType = swDocDRAWING) Then
'If doc is drawing then activate model and read properties from there
dwgdoc = Part.GetTitle()
Set view0 = Part.GetFirstView()
Set view1 = view0.GetNextView()
modelname = view1.GetReferencedModelName()
'this is the line where I get a type mismatch error
retval1 = swApp.ActivateDoc2(modelname, True, Error)
Set Part1 = swApp.ActiveDoc
.........................
.........................
.........................
'reactivate drawing
Part = swApp.ActivateDoc2(dwgdoc, True, Error)
Else
.........................
.........................
.........................
End If
I used ........ for lines that are not related to this problem.
The SolidWorks API help for the line with type mismatch problem says:
=========================================================
Description
This function activates a document which has already been loaded. This file becomes the active document and this method returns a pointer to that document object.
Syntax (OLE Automation)
retval = SldWorks.ActivateDoc2 ( name, silent, &errors )
Input:
(BSTR) name
Name of document to activate
Input:
(BOOL) silent
True if dialogs and warning messages should be avoided, False if dialogs and warning messages should be displayed to the user.
Output:
(long) errors
Status of the document activate operation, refer to the swActivateDocError_e enumeration. If no errors or warnings were encountered, this value will be set to 0.
Return:
(LPDISPATCH) retval
Pointer to a dispatch object, the document
==========================================================
What do I have to do to get this program running?
Andrew (Netshop21)
1) Open a drawing in SW.
2) Run a macro/program
3) The macro will determine the solid model used to create the drawing, will load and activate the model, will read the
custom properties from the model, will reactivate the drawing and will write the custom properties into the drawing.
I have difficulties in opening the model. The code I wrote looks like this:
Dim swApp As Object
Dim Part As Object
Dim retval As Boolean
Dim view0 As Object
Dim view1 As Object
Dim Part1 As Object
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
'Form1.Height = 4400
If Part Is Nothing Then
msg = "A SolidWorks document needs to be loaded!"
Style = vbExclamation ' Error style dialog
Titlemsg = "Model Properties" ' Define title
Call MsgBox(msg, Style, Titlemsg) ' Display error message
returnOK = False
swApp.Visible = True
End ' If no model currently loaded, then exit
Else
docType = Part.GetType
End If
.....................
.....................
.....................
If (docType = swDocDRAWING) Then
'If doc is drawing then activate model and read properties from there
dwgdoc = Part.GetTitle()
Set view0 = Part.GetFirstView()
Set view1 = view0.GetNextView()
modelname = view1.GetReferencedModelName()
'this is the line where I get a type mismatch error
retval1 = swApp.ActivateDoc2(modelname, True, Error)
Set Part1 = swApp.ActiveDoc
.........................
.........................
.........................
'reactivate drawing
Part = swApp.ActivateDoc2(dwgdoc, True, Error)
Else
.........................
.........................
.........................
End If
I used ........ for lines that are not related to this problem.
The SolidWorks API help for the line with type mismatch problem says:
=========================================================
Description
This function activates a document which has already been loaded. This file becomes the active document and this method returns a pointer to that document object.
Syntax (OLE Automation)
retval = SldWorks.ActivateDoc2 ( name, silent, &errors )
Input:
(BSTR) name
Name of document to activate
Input:
(BOOL) silent
True if dialogs and warning messages should be avoided, False if dialogs and warning messages should be displayed to the user.
Output:
(long) errors
Status of the document activate operation, refer to the swActivateDocError_e enumeration. If no errors or warnings were encountered, this value will be set to 0.
Return:
(LPDISPATCH) retval
Pointer to a dispatch object, the document
==========================================================
What do I have to do to get this program running?
Andrew (Netshop21)






RE: loading and activating a model from VB
Option Explicit
'<><><><><><><><><><><><><><>
' Transfer Custom Properties
'<><><><><><><><><><><><><><>
Dim swApp As Object
Dim Dwg As Object
Dim View As Object
Dim Model As Object
Const swDocDRAWING = 3
Dim sModelName As String
Sub Main()
Dim swError As Long
Set swApp = CreateObject("SldWorks.Application")
Set Dwg = swApp.ActiveDoc
'Verify a Drawing is Open
If (Dwg Is Nothing) Or (Dwg.GetType <> swDocDRAWING) Then
swApp.SendMsgToUser "You Must Have a Drawing Opened"
Exit Sub
End If
'Get Reference Model
Set View = Dwg.GetFirstView 'drawing template
Set View = View.GetNextView 'first drawing view
sModelName = View.GetReferencedModelName()
'Open the Reference Model, read the custom properties, save and close the model
Set Model = swApp.ActivateDoc2(sModelName, True, swError)
swApp.SendMsgToUser "Get the Custom Properties"
Model.Save2 True
swApp.CloseDoc sModelName
'Rebuild the drawing
swApp.SendMsgToUser "Write the Custom Properties to the Drawing"
Dwg.EditRebuild
'Clean Up
Set Model = Nothing
Set View = Nothing
Set Dwg = Nothing
Set swApp = Nothing
End Sub
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: loading and activating a model from VB
It doesn't work, I get the same type mismatch error at this line:
Set Model = swApp.ActivateDoc2(sModelName, True, swError)
Andrew
RE: loading and activating a model from VB
You may want to try this method in place of ActivateDoc2:
Set Model = swApp.ActivateDoc(sModelName)
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: loading and activating a model from VB
It does not in Visual Basic 6.0. This is part of a more complex program written in VB.
Andrew
RE: loading and activating a model from VB
It works just fine from VB6 as well. I just created a new project, included a reference to the SolidWorks Type Library, added a form with a command button and used this code.
Option Explicit
'<><><><><><><><><><><><><><>
' Transfer Custom Properties
'<><><><><><><><><><><><><><>
Dim swApp As Object
Dim Dwg As Object
Dim View As Object
Dim Model As Object
Const swDocDRAWING = 3
Dim sModelName As String
Private Sub Command1_Click()
Me.Hide
Call DoStuff
Unload Me
End
End Sub
Sub DoStuff()
Dim swError As Long
Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
Set Dwg = swApp.ActiveDoc
'Verify a Drawing is Open
If (Dwg Is Nothing) Or (Dwg.GetType <> swDocDRAWING) Then
swApp.SendMsgToUser "You Must Have a Drawing Opened"
Exit Sub
End If
'Get Reference Model
Set View = Dwg.GetFirstView 'drawing template
Set View = View.GetNextView 'first drawing view
sModelName = View.GetReferencedModelName()
'Open the Reference Model, read the custom properties, save and close the model
Set Model = swApp.ActivateDoc2(sModelName, True, swError)
swApp.SendMsgToUser "Get the Custom Properties"
Model.Save2 True
swApp.CloseDoc sModelName
'Rebuild the drawing
swApp.SendMsgToUser "Write the Custom Properties to the Drawing"
Dwg.EditRebuild
'Clean Up
Set Model = Nothing
Set View = Nothing
Set Dwg = Nothing
Set swApp = Nothing
End Sub
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: loading and activating a model from VB
Two stars for you.
One more question tough. In the SolidWorks API Help it says:
retval = SldWorks.ActivateDoc2 ( name, silent, &errors )
You are using:
Set Model = swApp.ActivateDoc2(sModelName, True, swError)
"Set" is not mentioned in SW API Help. How can one figure out that it should be used?
Andrew
RE: loading and activating a model from VB
Return: (LPDISPATCH) retval
Pointer to a dispatch object, the document
The help file indicates that it will return a pointer to an object. VB requires that you use the Set command for initializing object variables. SolidWorks dows illustrate this in their sample code, but that is not available for every subject.
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: loading and activating a model from VB
Andrew
RE: loading and activating a model from VB
Thread559-13735
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.