VBA Code to return Body Name from part within an assembly

VBA Code to return Body Name from part within an assembly

Paul1084
Advocate Advocate
924 Views
4 Replies
Message 1 of 5

VBA Code to return Body Name from part within an assembly

Paul1084
Advocate
Advocate

HI,

 

I am struggling with some code to initially return a Body name from a Part placed in an assembly... the code must run from the assembly file... if I run the code in the Part, it works fine and returns the selected body name. when I run it in the Assmbly it does not return the name, however it does not come up with an error either... I think I need to reference the selected part, and not the active application when selecting the body... hence the code I have added to select the part first... but I dont know how to do this... I can get it to print the parent name... i.e. Debug.Print Body.parent.name... but this doesn't help me unfortunately

 

 

Sub INSERT_D1()

'Make a ref to active doc
Dim doc As Document
Set doc = ThisApplication.ActiveDocument

 

Dim Structure As ComponentOccurrence
Dim Structuredoc As Document

 

Set Structure = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the structure")
Set Structuredoc = Structure.Definition.Document

 

    Dim body As SurfaceBody
    Set body = ThisApplication.CommandManager.Pick(kPartBodyFilter, "Select wall to insert door on.")

 

Debug.Print body.Name

 

 

End Sub

0 Likes
925 Views
4 Replies
Replies (4)
Message 2 of 5

MechMachineMan
Advisor
Advisor

I'm almost certain the issue you are running into here is because of proxies;

 

Either the Pick command isn't selecting the proxy. OR the rest of the code IS grabbing the proxy, and you are not accessing the actual body. 

 

You will need to look into proxies more to be able to get what you are looking for.

 

(The information about the body is only stored in the part as a body, but is accessed indirectly through the assembly via proxies)


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 5

LishuangLu
Autodesk
Autodesk

Hi Paul, 

I tested your code in my side, it works fine. See the video from me. 

http://autode.sk/2qVy4BZ

 

Let me know if anything was missing by me, or the problem is related with typical assembly dataset. 

 

Thanks, 

-Lisa

 

 

Message 4 of 5

Paul1084
Advocate
Advocate

Thanks for this Lisa... your video demonstrates exactly how I wanted & expected it to work... it is bizarre that it has worked for your assembly, but not mine.

 

I have however found a work around, that will achieve the same end goal for my code, just using the feature name instead of the body:

 

 

Sub INSERT_D1()

 

'Make a ref to active doc
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument

 

Dim oDef As AssemblyComponentDefinition
Set oDef = doc.ComponentDefinition

 

Dim body As PartFeature
Set body = ThisApplication.CommandManager.Pick(kPartFeatureFilter, "Select wall to insert door on.")

 

Debug.Print Body.Name

 

End Sub

0 Likes
Message 5 of 5

LishuangLu
Autodesk
Autodesk

Hi Paul,

 

Good to know you have found the workaround to meet your requirement.

If you still prefer accessing body, then you may share the assembly with me for investigation.

 

Thanks,

-Lisa

 

0 Likes