Activate Referenced Model Opened from Drawing - VBA

Activate Referenced Model Opened from Drawing - VBA

eric.frissell26WKQ
Advocate Advocate
939 Views
4 Replies
Message 1 of 5

Activate Referenced Model Opened from Drawing - VBA

eric.frissell26WKQ
Advocate
Advocate

I'm trying to write a VBA macro to fill in dates on our models and drawings (because opening iProperties, clicking through the property update menus, just to check 'date' is too much work) and I'm running into a roadblock figuring out how to activate the now open document.  Idea is you run this from the drawing and it re-writes all the dates in the model and drawing - it's worked for the drawing but now I'm expanding it.  Here's the code

 

 

Sub UpdateDrawPropertiesV2()

Dim invDraw As DrawingDocument
Set invDraw = ThisApplication.ActiveDocument

Dim oSheet As Sheet
Dim oDrawingView As DrawingView
Dim oModelDoc As Document


Dim oPropSet As PropertySet
Dim oStatusSet As PropertySet
Dim oProjSet As PropertySet
Set oPropSet = invDraw.PropertySets.Item("Inventor User Defined Properties")
Set oStatusSet = invDraw.PropertySets.Item("Design Tracking Properties")
Set oSumSet = invDraw.PropertySets.Item("Inventor Summary Information")


'Add Drawn By
Dim invDrawDes As Property
Dim invDrawDesigner As String
invDrawDesigner = InputBox("Enter Drawn By Name", drawnby, Default)
Set invDrawDes = oSumSet.ItemByPropId(kAuthorSummaryInformation)
invDrawDes.Value = invDrawDesigner

'Add Drawn By Date
'Debug.Print (Date)
Dim invDrawDate As Property
Set invDrawDate = oStatusSet.ItemByPropId(kCreationDateDesignTrackingProperties)
invDrawDate.Value = Date

invDraw.Save

'Add Designed By Date
Dim invDesignDate As Property
For Each oSheet In invDraw.Sheets
oSheet.Activate
Set oDrawingView = oSheet.DrawingViews.Item(1)
Set oModelDoc = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
Call ThisApplication.Documents.Open(oModelDoc.FullDocumentName, True) ' False for invisbly opened

'Need to activate model here'

'Set invDesignDate = ThisApplication.OpenoStatusSet.ItemByPropId(kCreationDateDesignTrackingProperties)


Next
oModelDoc.Save

End Sub

 

 

Calling ThisApplication.Documents....... may not be the most efficient way to do this so if anyone has any suggestions I'd really appreciate it. 

 

Also if anyone has any suggestions on how to set up part/assembly/drawing templates so that things like creation date are linked to first check in that'd be pretty slick too.

0 Likes
Accepted solutions (1)
940 Views
4 Replies
Replies (4)
Message 2 of 5

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

Some questions:

- There are 2 or more models referenced in your drawing?

- There is always only one relevant view on every sheet?

I think you don't need to activate each sheet in drawing. The referenced models are already opened in background if you have the drawing open. No need to open it again.

I have not tested, but try to do:

...

'Add Designed By Date
Dim invDesignDate As Property
For Each oSheet In invDraw.Sheets
    Set oDrawingView = oSheet.DrawingViews.Item(1)
    Set oModelDoc = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
    Set oStatusSet = oModelDoc.PropertySets.Item("Design Tracking Properties")
    Set invDesignDate = oStatusSet.ItemByPropId(kCreationDateDesignTrackingProperties)
    invDesignDate.Value = Date
Next

 Save the drawing after all property operations with

invDraw.Save2

This will also save all dependent dirty documents.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 5

eric.frissell26WKQ
Advocate
Advocate

Thanks Krieg!

 

My model can be either part or assembly - if this is the case would there need to be 2 conditions for this?

 

A significant part of the time the drawing only references 1 model

 

I agree with you the 'for each Sheet' is likely overkill, I played around for a few minutes and couldn't find a way to to open the document from the drawing, so it's what I found on the forum.  I'm all ears if you know a more direct way to open the referenced model

 

I'll play with this in a little bit and let you know how it goes, thanks for the help!

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

You can check the Count property of the drawing's AllReferencedDocuments property to see if there is more than one.  If it is zero, there is no 'model' document yet.  If it is one, then you can just get Item(1) as the model document.  If it is more than one, then you can just loop through each document in the drawing's AllReferencedDocuments collection.  That way you avoid having to loop through each sheet and/or each view on each sheet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

eric.frissell26WKQ
Advocate
Advocate

@Ralf_Krieg nailed it! The suggestion worked perfectly, thanks!!

0 Likes