×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

loading and activating a model from VB

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)

RE: loading and activating a model from VB

Try this tidbit. I cleaned up some of your procedures as well.

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

(OP)
dsi,
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

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:

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

(OP)
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

RE: loading and activating a model from VB

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.

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

(OP)
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)

"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

(OP)
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

RE: loading and activating a model from VB

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources