VBA code only working in some .idws and with some .iams

VBA code only working in some .idws and with some .iams

RNDinov8r
Collaborator Collaborator
631 Views
8 Replies
Message 1 of 9

VBA code only working in some .idws and with some .iams

RNDinov8r
Collaborator
Collaborator

So, I was able to work with some code a number of you had helped me with about a week ago. Everything seemed to be going fine with my occurrence tracking/labeling until I would put a certain sub-assembly into the "main" assembly. I started to look at the difference between that sub and other subs in the assembly and can find no discernable difference as far as settings go. Then for yucks, I tried the VBA code on a new .idw file (my code is in a standard macro/VBA form that opens when I open inventor). Turns out, when I run the VBA in the new idw it does not create the table at all, with a main assemlby that was working in a previous drawing. What am I missing. It doesn't error out either, just never creates a new table.

 

So the idea is that the code reads through an assembly, and burrows down through it. It looks for all occurrences that aren't integers but are infact text. This is how we label instances of the same sensors and proxes, etc. This is how I determine my qty of inputs on a machine (and outputs) as well as make tags which are then given to controls department...it keeps continuity in naming...

 

So, I am seeing two issues. 

1)  Why does this code only seem to work in one drawing? When I create a new drawing and put the "main" assembly in it, it does not create a table. It only works in the existing drawing I have. (of which I created an attached screen shot).

 

2) Why does it not work even in the "good" drawing if certain sub assemblies are present? (as soon as i delete them, it will create the table for me).

 

Unfortunately, I cannot share any of the actual CAD files due to proprietary issues with our customer.

 

As near as I can tell, this code does have to run in the .idw with the BOM sturcture set to Parts Only.

 

Here's the code:

Public Sub CreateNewCustomTable()

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oDoc.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

Dim oDef As AssemblyComponentDefinition
Set oDef = oAsmDoc.ComponentDefinition

Dim sensorCnt As Integer
Dim oContents() As String
Dim IsInteger As Boolean
Dim LArray() As String
Dim occ As ComponentOccurrence
For Each occ In oDef.Occurrences.AllLeafOccurrences
On Error Resume Next
LArray = Split(occ.Name, ":")

IsInteger = ConvertToInteger(LArray(1))

If IsInteger = False Then
sensorCnt = sensorCnt + 1
End If
Next

If sensorCnt > 0 Then
sensorCnt = sensorCnt * 2
ReDim oContents(sensorCnt - 1) As String
Dim i As Integer
i = 0
For Each occ In oDef.Occurrences.AllLeafOccurrences
LArray = Split(occ.Name, ":")
IsInteger = ConvertToInteger(LArray(1))
If IsInteger = False Then
oContents(i) = LArray(0)
oContents(i + 1) = LArray(1)
i = i + 2
End If
Next
End If

' Set a reference to the active sheet.
Dim oSheet As Sheet
Set oSheet = oDoc.ActiveSheet

' Set the column titles
Dim oTitles(1 To 2) As String
oTitles(1) = "Part Number"
oTitles(2) = "Sensor Description"


' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(1 To 2) As Double
oColumnWidths(1) = 4.5
oColumnWidths(2) = 5.5

oDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.TableStyle.HeadingGap = 0.1

' Create the custom table
Dim oCustomTable As CustomTables
Set oCustomTable = oSheet.CustomTables.Add("Input Locations & Descriptions", ThisApplication.TransientGeometry.CreatePoint2d(32.7, 27), _
2, sensorCnt / 2, oTitles, oContents, oColumnWidths)

' Change the 3rd column to be left justified.
oCustomTable.Columns.Item(1).ValueHorizontalJustification = kAlignTextLeft
oCustomTable.Columns.Item(2).ValueHorizontalJustification = kAlignTextLeft

' Create a table format object
Dim oFormat As TableFormat
Set oFormat = oSheet.CustomTables.CreateTableFormat

' Set inside line color to red.
oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)

' Set outside line weight.
oFormat.OutsideLineWeight = 0.05

' Modify the table formats
oCustomTable.OverrideFormat = oFormat


End Sub

Function ConvertToInteger(v1 As String) As Boolean
On Error GoTo 100:
Dim i As Integer
i = CInt(v1)
ConvertToInteger = True
Exit Function
100:
ConvertToInteger = False
End Function

0 Likes
632 Views
8 Replies
Replies (8)
Message 2 of 9

MechMachineMan
Advisor
Advisor

FTFY - Nesting and Code Box...

 

In your code, comment out the On Error Resume Next lines and run it though the vba. It'll point out pretty quick where your issues are happening in the VBA version.

 

 

Public Sub CreateNewCustomTable()

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oDoc.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

Dim oDef As AssemblyComponentDefinition
Set oDef = oAsmDoc.ComponentDefinition

Dim sensorCnt As Integer
Dim oContents() As String
Dim IsInteger As Boolean
Dim LArray() As String
Dim occ As ComponentOccurrence
For Each occ In oDef.Occurrences.AllLeafOccurrences On Error Resume Next LArray = Split(occ.Name, ":") IsInteger = ConvertToInteger(LArray(1)) If IsInteger = False Then sensorCnt = sensorCnt + 1 End If Next If sensorCnt > 0 Then sensorCnt = sensorCnt * 2 ReDim oContents(sensorCnt - 1) As String
Dim i As Integer i = 0
For Each occ In oDef.Occurrences.AllLeafOccurrences LArray = Split(occ.Name, ":") IsInteger = ConvertToInteger(LArray(1))
If IsInteger = False Then oContents(i) = LArray(0) oContents(i + 1) = LArray(1) i = i + 2 End If Next End If Dim oSheet As Sheet Set oSheet = oDoc.ActiveSheet Dim oTitles(1 To 2) As String oTitles(1) = "Part Number" oTitles(2) = "Sensor Description" Dim oColumnWidths(1 To 2) As Double oColumnWidths(1) = 4.5 oColumnWidths(2) = 5.5 oDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.TableStyle.HeadingGap = 0.1 Dim oCustomTable As CustomTables Set oCustomTable = oSheet.CustomTables.Add("Input Locations & Descriptions", ThisApplication.TransientGeometry.CreatePoint2d(32.7, 27), _ 2, sensorCnt / 2, oTitles, oContents, oColumnWidths) oCustomTable.Columns.Item(1).ValueHorizontalJustification = kAlignTextLeft oCustomTable.Columns.Item(2).ValueHorizontalJustification = kAlignTextLeft Dim oFormat As TableFormat Set oFormat = oSheet.CustomTables.CreateTableFormat oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0) oFormat.OutsideLineWeight = 0.05 oCustomTable.OverrideFormat = oFormat End Sub Function ConvertToInteger(v1 As String) As Boolean On Error GoTo 100: Dim i As Integer i = CInt(v1) ConvertToInteger = True Exit Function 100: ConvertToInteger = False End Function

--------------------------------------
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 9

RNDinov8r
Collaborator
Collaborator

I have attached the screen shots of the issues i get. They mean nothing to me. (I know very little VBA...especially in inventor).

 

from what I can tell (and I could be way off) "Dim LArray" is an actual array which is calling for 2 (0 &1) dimensions after it's parcing the [file name and occurrence "descriptor"] name and checking to see if the second half "1" ( 2nd dimension) is an integer. Since it's boolean it will either return true of false.  I can't tell what happens if the boolean returns as True though..I only see it looks for a false condition.

 

If it's true, I would think it jumps down to the next if statement...so if my first instance of a part isn't an integer...is that causing the problem?

0 Likes
Message 4 of 9

MechMachineMan
Advisor
Advisor

So uh, yeah. It's a 0 based array., so it's filled 0 then 1 then 2, etc.

 

If it's having indexing problems it's telling you there isn't a value in the "1" index.

 

Looking at what makes up the array, we see it's a split.

 

If Split finds the character, it splits that string into however many pieces in order to split at the char.

 

In this case, the error and the split is telling us that not all of your occurrence names have a ":" symbol in them.

 

What you need to do is write some quick code to ensure the occurrence name has one in before you use the split function on it.


--------------------------------------
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 5 of 9

RNDinov8r
Collaborator
Collaborator

So, I went back through ALL of my models. All have the ":" instance in them...so this was not the issue.

 

Haven't had a chance to dive back into in the last few days...need to look at it again.

0 Likes
Message 6 of 9

MechMachineMan
Advisor
Advisor

All Nodes are occurrences, but not all occurrences are nodes.

 

If you went through it visually, you are likely missing the "weld bead occurrences" that are hidden but exist within Weldment files.


--------------------------------------
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 7 of 9

MechMachineMan
Advisor
Advisor

Here's a version with the relevant ~6 lines of code added in to figure it out...

 

Public Sub CreateNewCustomTable()

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oDoc.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

Dim oDef As AssemblyComponentDefinition
Set oDef = oAsmDoc.ComponentDefinition

Dim sensorCnt As Integer
Dim oContents() As String
Dim IsInteger As Boolean
Dim LArray() As String
Dim occ As ComponentOccurrence
For Each occ In oDef.Occurrences.AllLeafOccurrences On Error Resume Next LArray = Split(occ.Name, ":")
If Err.Number <> 0 Then
Call MsgBox("LArray issue with split! & vblf & "Occ Name: " & occ.Name & vblf & vblf & "ABORTING RULE!")
Exit SUb
End if
IsInteger = ConvertToInteger(LArray(1))

If Err.Number <> 0 Then
Call MsgBox("Error Converting to Integer!" & vblf & "The argument passed to the ConvertToInteger Function was:" & vblf & vblf & LArray(1)) Exit Sub
End if
If IsInteger = False Then sensorCnt = sensorCnt + 1 End If Next If sensorCnt > 0 Then sensorCnt = sensorCnt * 2 ReDim oContents(sensorCnt - 1) As String
Dim i As Integer i = 0
For Each occ In oDef.Occurrences.AllLeafOccurrences LArray = Split(occ.Name, ":") IsInteger = ConvertToInteger(LArray(1))
If IsInteger = False Then oContents(i) = LArray(0) oContents(i + 1) = LArray(1) i = i + 2 End If Next End If Dim oSheet As Sheet Set oSheet = oDoc.ActiveSheet Dim oTitles(1 To 2) As String oTitles(1) = "Part Number" oTitles(2) = "Sensor Description" Dim oColumnWidths(1 To 2) As Double oColumnWidths(1) = 4.5 oColumnWidths(2) = 5.5 oDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.TableStyle.HeadingGap = 0.1 Dim oCustomTable As CustomTables Set oCustomTable = oSheet.CustomTables.Add("Input Locations & Descriptions", ThisApplication.TransientGeometry.CreatePoint2d(32.7, 27), _ 2, sensorCnt / 2, oTitles, oContents, oColumnWidths) oCustomTable.Columns.Item(1).ValueHorizontalJustification = kAlignTextLeft oCustomTable.Columns.Item(2).ValueHorizontalJustification = kAlignTextLeft Dim oFormat As TableFormat Set oFormat = oSheet.CustomTables.CreateTableFormat oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0) oFormat.OutsideLineWeight = 0.05 oCustomTable.OverrideFormat = oFormat End Sub Function ConvertToInteger(v1 As String) As Boolean On Error GoTo 100: Dim i As Integer i = CInt(v1) ConvertToInteger = True Exit Function 100: ConvertToInteger = False End Function

 


--------------------------------------
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 8 of 9

RNDinov8r
Collaborator
Collaborator

Thanks MechMachineMan,

 

Hopefully I can play around with it this afternoon.

 

 

0 Likes
Message 9 of 9

chandra.shekar.g
Autodesk Support
Autodesk Support

@MechMachineMan - Thanks for improvement in code.

 

@RNDinov8r - Hopefully, suggestion from @MechMachineMan would help.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes