Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading and activating a model from VB 1

Status
Not open for further replies.

dogarila

Mechanical
Oct 28, 2001
594
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)

 
Replies continue below

Recommended for you

Try this tidbit. I cleaned up some of your procedures as well.
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

Sub Main()
    Dim swError As Long
    Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
    Set Dwg = swApp.ActiveDoc
    'Verify a Drawing is Open
    If (Dwg Is Nothing) Or (Dwg.GetType <> swDocDRAWING) Then
        swApp.SendMsgToUser &quot;You Must Have a Drawing Opened&quot;
        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 &quot;Get the Custom Properties&quot;
    Model.Save2 True
    swApp.CloseDoc sModelName
    'Rebuild the drawing
    swApp.SendMsgToUser &quot;Write the Custom Properties to the Drawing&quot;
    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.
 
dsi,
It doesn't work, I get the same type mismatch error at this line:

Set Model = swApp.ActivateDoc2(sModelName, True, swError)

Andrew
 
What version of SW are you using? If my code is placed in a macro by iteself (SW2001 SP11), there are no problems. Make sure that you check your objects. Notice that I have an object for the drawing and one for the model.

You may want to try this method in place of ActivateDoc2:
Code:
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.
 
Yes, you are right, it works as a macro in SW.
It does not in Visual Basic 6.0. This is part of a more complex program written in VB.

Andrew
 
Andrew:

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.
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(&quot;SldWorks.Application&quot;)
    swApp.Visible = True
    Set Dwg = swApp.ActiveDoc
    'Verify a Drawing is Open
    If (Dwg Is Nothing) Or (Dwg.GetType <> swDocDRAWING) Then
        swApp.SendMsgToUser &quot;You Must Have a Drawing Opened&quot;
        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 &quot;Get the Custom Properties&quot;
    Model.Save2 True
    swApp.CloseDoc sModelName
    'Rebuild the drawing
    swApp.SendMsgToUser &quot;Write the Custom Properties to the Drawing&quot;
    Dwg.EditRebuild
    'Clean Up
    Set Model = Nothing
    Set View = Nothing
    Set Dwg = Nothing
    Set swApp = Nothing

End Sub
[code]

 DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
I fixed it. It works beautifully. I deserve to slap myself. The problem was that I forgot to declare swError as long.
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)

&quot;Set&quot; is not mentioned in SW API Help. How can one figure out that it should be used?

Andrew
 

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.
 
One more little thing. The program always closes the solid model to switch back to the drawing (even if it was open before). Is it possible to keep the model open if it was open before and close it if it wasn't?

Andrew
 
Oops! I started a new thread with your answer.
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor