Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Retrieve Slected Dimension in View with VBA

13 REPLIES 13
Reply
Message 1 of 14
Anonymous
1523 Views, 13 Replies

Retrieve Slected Dimension in View with VBA

I am using Inventor Pro 2014. For several years now, I have been using a VBA macro to create an .idw file with drawing views of a part or assembly. I'm now trying to go to the next step and retrieve selected dimensions for the part or components of the assembly and place in the .idw view.

 

I found this code in an earlier post:

 

 

 

Private Sub RetrieveTest_1()
  
  Dim oDrawDoc As DrawingDocument
  Set oDrawDoc = ThisApplication.ActiveDocument
  
  Dim oSheet As Sheet
  Set oSheet = oDrawDoc.Sheets.Item(1)
  
  Dim oView As DrawingView
  Set oView = oSheet.DrawingViews.Item(1)
  
  'object collection for dimensions to be retrieved
  Dim oTO As TransientObjects
  Set oTO = ThisApplication.TransientObjects
  Dim oObjColl As ObjectCollection
  Set oObjColl = oTO.CreateObjectCollection
  
  'fill the object collection with some dimension constrains
  'Here we use sketch DimensionConstraint objects.
  Dim oDoc As PartDocument
  Set oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
  Dim oDef As PartComponentDefinition
  Set oDef = oDoc.ComponentDefinition
  Dim oDimConstraints As DimensionConstraints
  Set oDimConstraints = oDef.Sketches.Item(1).DimensionConstraints
  
  Dim oDC As DimensionConstraint
  'add first two constraints to the collection
  Set oDC = oDimConstraints.Item(1)
  MsgBox (oDC.Parameter.Name)
  Call oObjColl.Add(oDC)
  'Set oDC = oDimConstraints.Item(2)
  'Call oObjColl.Add(oDC)
  
  'retrieve subset of dimensions
   On Error Resume Next
   Call oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView, oObjColl)
   MsgBox (Error)
   On Error GoTo 0

'  'retrieve all the dimensions
'   Call oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView)

End Sub 'RetrieveTest_1

 

 

When I try to run this, it gives an error on the line, "Call oSheet.DrawingsDim ...." The error is "Method 'Retrieve' of object 'GeneralDimensions' failed."

 

oDC is being found, and the MsgBox is returning the proper name of a dimension in the part. Also, it will retrieve all of the dimensions.

 

Any idea what's going on here? Perhaps retrieve all the dims and sort them to get rid of the ones that don't work?

 

Thanks,

Tim Rumph

 

13 REPLIES 13
Message 2 of 14
adam.nagy
in reply to: Anonymous

Hi Tim,

 

Could you try to use GetRetrievableDimensions() and then pass the returned ObjectCollection to Retrieve()?

 

I wonder if that helps.

 

Cheers,



Adam Nagy
Autodesk Platform Services
Message 3 of 14
Balaji_Ram
in reply to: Anonymous

Hi Tim,

 

I did not have Inventor 2014 in hand to test your code, but i can confirm that it did work ok in Inventor 2016.

 

This was the code which is mostly from your post :

 

Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    If oDoc.DocumentType <> kDrawingDocumentObject Then Exit Sub
            
    Dim oDwgDoc As DrawingDocument
    Set oDwgDoc = oDoc
    
    Dim oSheet As Sheet
    Set oSheet = oDwgDoc.Sheets.Item(1)
  
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews.Item(1)
  
    Dim oTO As TransientObjects
    Set oTO = ThisApplication.TransientObjects
   
    Dim oPartDoc As PartDocument
    Set oPartDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
    
    Dim oDef As PartComponentDefinition
    Set oDef = oPartDoc.ComponentDefinition
    
    Dim oDimConstraints As DimensionConstraints
    Set oDimConstraints = oDef.Sketches.Item(1).DimensionConstraints
  
    Dim oObjColl As ObjectCollection
    Set oObjColl = oTO.CreateObjectCollection
    
    Dim oDC As DimensionConstraint
    Set oDC = oDimConstraints.Item(1)
    Call oObjColl.Add(oDC)
    
    Set oDC = oDimConstraints.Item(2)
    Call oObjColl.Add(oDC)
   
    Call oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView, oObjColl)

 

Is the error that you are getting while retrieving, specific to a part ? 

Can you please try creating a simple rectangular extrusion with two dimensional constraints in its sketch ?

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 4 of 14
Anonymous
in reply to: Balaji_Ram

I figured out what was happening. The first dimension was a radius that was not in the view I had created, but was perpendiculat to it. Smiley Embarassed Once I tried another view, it worked fine.

Message 5 of 14
Balaji_Ram
in reply to: Anonymous

Glad you have got it working.

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Message 6 of 14
Anonymous
in reply to: adam.nagy

Okay, that works too. But, (you knew there'd be one right?) most of what I deal with is assemblies mostly made of derived parts. We do compression molding of rubber based parts onto stamped steel plates. If I use "...GeneralDimensions.Retrieve(oView)" it pulls in all of the dimensions from the parent part, but if I use Adams suggestion of "GetRetrievableDimensions()" or the original code, it returns nothing.

 

Bringing all of the dimensions in and getting rid of the ones I don't want is more work than just dimensioning the part from scratch. The dimensions I want to bring in are named. Any suggestions on how to select them from the parent of a derived part?

 

Thanks,

Tim

Message 7 of 14
adam.nagy
in reply to: Anonymous

Hi Tim,

 

Could you please provide a minimal non-confidential document that can be used to reproduce the issue on our side:

1) that the GetRetrievableDimensions() does not return anything

2) that GeneralDimensions.Retrieve(oView) errors out

 

Thank you,



Adam Nagy
Autodesk Platform Services
Message 8 of 14
Anonymous
in reply to: adam.nagy

Thanks Adam;

 

I've attached a file, DimTest.zip, with an example part. DTP-1.ipt and DTP-2.ipt are parts, which are included in the assembly DTP-3.iam. DTP-4.ipt is a derived part made from DTP-3.iam by subtracting DPT-1 from DTP-2. DTP-4.idw is the drawing of DTP-4.ipt. Because of the nature of our products, many of our Inventor models are done this way.

 

When I use this, "Call oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView)" it retrieves all of the dimensions, as shown in the file "DTP-4 Retrieve All.pdf". Note that there are NO dimensions in DTP-4 (this is also typical for our products). These dimension are in DTP-1 and DTP-2.

 

When I use this, "Set oDimObj = oSheet.DrawingDimensions.GeneralDimensions.GetRetrievableDimensions(oView)", it returns nothing. oDimObj is empty. (Note: Dim oDimObj As ObjectCollection)

 

I would like to get the collection of dimensions and sort through it by dimension name to select the dimensions that appear in different views and move them to the proper locations in the drawing. I am using Inventor Pro 2014.

 

Thank you,

Tim Rumph

Message 9 of 14
adam.nagy
in reply to: Anonymous

Thanks, Tim.

 

Sorry, I forgot to follow up on this. 

 

I have logged the problem with GetRetrievableDimensions in case of derived parts: #88333. We'll see what can be done about that.

The other issue is that the RetrievedFrom property for the dimensions retrieved is not set either, so I'm not sure how you could tell where exactly that dimension is coming from in case of a derived part. 

 

I'll ask around if others have any suggestions - otherwise as you said, maybe it's easier to recreate them in the drawing.

 

Cheers,



Adam Nagy
Autodesk Platform Services
Message 10 of 14
Anonymous
in reply to: adam.nagy

Thanks Adam. As far as your question is concerned, the part in the drawing had no dimensions, they all had to come from the parent parts.

 

Tim






Message 11 of 14
adam.nagy
in reply to: Anonymous

Hi Tim,

 

I was playing around with things and this seemed to work:

http://adndevblog.typepad.com/manufacturing/2015/08/retrieve-sketch-dimension-of-part-document-insid...

 

Cheers,



Adam Nagy
Autodesk Platform Services
Message 12 of 14
Anonymous
in reply to: adam.nagy

Thanks Adam. I'm doing some training this week, but I'll try it out next week and let you know how it goes.

Tim Rumph

This email and any attachments are only for use by the intended recipient(s) and may contain legally privileged, confidential, proprietary or otherwise private information. Any unauthorized use, reproduction, dissemination, distribution or other disclosure of the contents of this e-mail or its attachments is strictly prohibited. If you have received this email in error, please notify the sender immediately and delete the original. Neither this information block, the typed name of the sender, nor anything else in this message is intended to constitute an electronic signature unless a specific statement to the contrary is included in this message.
Message 13 of 14
Balaji_Ram
in reply to: Anonymous

Hi Tim,

 

I have tried the code snippet that Adam shared and it did work ok.

 

But please note that i have come across a behavior while retrieving dimensions which you may also observe :

-When trying to retrieve the dimension one at a time from the retrievable dimensions collection, it worked ok.

-When one of the dimension from the collection was picked randomly from that collection, all the dimensions were retrieved.

 

I have logged this in our internal database for our engineering team to analyze.

 

The following workaround was used to retain only a few dimensions :

http://adndevblog.typepad.com/manufacturing/2015/07/retrieving-dimensions-in-a-drawing-view.html

 

Regards,

Balaji

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 14 of 14
Anonymous
in reply to: Balaji_Ram

Adam and Balaji;

 

Thanks for your help. Adam's code worked fine on a derived PART. That was step one. 🙂 Step two is to get it to work on an assembly that contains a derived part (which is the actual case that I have).

 

I added this little bit to figure out if it was a part or assembly:

 

' Get the referenced part or assembly
  Dim oInsDoc As Document
  Dim oDoc As PartDocument
  Set oInsDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
 
  ' Figure out what type of document this is
  If oInsDoc.DocumentType = kPartDocumentObject Then
    Set oDoc = oInsDoc 'This is a part document, use it
  Else 'This is an assembly document, get a part
    Set oDoc = oInsDoc.ReferencedDocuments.Item(1) 'Get a derived part
  End If
 
  'What have we got?
  MsgBox (oDoc.FullDocumentName)

 

This worked fine until the very end. It plucked the dimension from the part in the parent assembly, and the message box that gave the dimension name showed that it had the correct (or a correct) dimension. When it got to this part at the very end:

 

' Retrieve subset of dimensions
  Call oSheet.DrawingDimensions.GeneralDimensions.Retrieve( _
    oView, oObjColl)

 

it had an error:

 

Run-time error '-2147467259 (80004005)':

Method 'Retrieve' of object 'GeneralDimensions' failed

 

Again, this error only occured in the view with the assembly. It worked fine with the derived part.

 

I'll try Balaji's sledge hammer next. Thanks for your help.

 

Tim

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Autodesk Design & Make Report