Add a new row to ipart table with a rule

Add a new row to ipart table with a rule

e2000773
Enthusiast Enthusiast
1,308 Views
9 Replies
Message 1 of 10

Add a new row to ipart table with a rule

e2000773
Enthusiast
Enthusiast

I am looking for a solution to add a new row to iPart when one parameter changes.

There is a row with:

 

Member: Side 1400, Part number: Side Metal 1400, Length: 1400, Height: 312

 

So if for an example I have a form where I change length to 1500 and next to it is a button from rule to add new row automatically.

 

The next row would be with a member and part number based on length:
Member: Side 1500, Part number: Side Metal 1500, Length: 1500, Height: 312

0 Likes
Accepted solutions (1)
1,309 Views
9 Replies
Replies (9)
Message 2 of 10

A.Acheson
Mentor
Mentor

Hi @e2000773 

Are these iparts intended to be custom lengths and added to the factory file each time or can you fill out the table of lengths and diameters in advance? If you can fully define the iPart then you can make an iAssembly to switch each member and then if you wanted to control the iAssembly members via form that is an easier option. 

 

Linking an adding factory iPart members is a longer more difficult route as I believe you need to use the API route. 

 

Can you give an example of the overall file structure?

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 10

WCrihfield
Mentor
Mentor

In addition to what Alan mentioned, if both your member name and part number are identical except for the length value, then I don't think you would be able to have another member with the same length, but different height, because they would both have the same member name and part number as the other member, which I don't think is allowed.  Plus, in order to create a new member (new row) you would need to specify an existing row (one way or another) to copy, in order to get a new row, then change the length value in that new row.  But it seems to me like the naming convention portion could be handles by formatting those cells that way while editing that iPart table in Excel.  That seems like it would be a simpler one time fix to avoid lots of extra code or manual name changes later on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 10

e2000773
Enthusiast
Enthusiast

Ok based on your comments this isn't going to work.

0 Likes
Message 5 of 10

e2000773
Enthusiast
Enthusiast

Ok I found a promising code:

Dim oFactoryDoc As PartDocument
oFactoryDoc = ThisApplication.ActiveDocument

Dim oPartFactory As iPartFactory
oPartFactory = oFactoryDoc.ComponentDefinition.iPartFactory

oWS = oPartFactory.ExcelWorkSheet
oWS.Cells(3, 1) = "Name=" & Length
oWS.Cells(3, 2) = "Name=" & Length
oWS.Cells(3, 3) = Lenght
oWS.Cells(3, 4) = Heigth

oWS.Parent.Save
oWS.Parent.Close

 This creates one new row, but when I run the rule again it doesn't create another. What is missing? 

0 Likes
Message 6 of 10

A.Acheson
Mentor
Mentor

Hi @e2000773 

You will need to set index the next member row. Your creating values in row 3 and column 1-4.

 

Here is row 4

oWS.Cells(4, 1) = "Name=" & Length
oWS.Cells(4, 2) = "Name=" & Length
oWS.Cells(4, 3) = Lenght
oWS.Cells(4, 4) = Heigth

But you may need to add multiples which is tedious or find the last row to add 1 dynamically.

 

Dim oFactoryDoc As PartDocument
oFactoryDoc = ThisApplication.ActiveDocument

Dim oPartFactory As iPartFactory
oPartFactory = oFactoryDoc.ComponentDefinition.iPartFactory

oWS = oPartFactory.ExcelWorkSheet

' Get Last Row
Dim LastRow As Integer
LastRow = oWS.Cells(oWS.Rows.Count, 1).End(xlUp).row
MessageBox.Show("LastRowNumber: " & LastRow)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 7 of 10

WCrihfield
Mentor
Mentor

Hi @e2000773.  When editing in Excel you have more freedom and capability than editing using the built-in dialog, but it can also be more complicated if you are not familiar with how to control things in the Excel environment.  From the looks of it, you probably only had 1 member row before running your rule, so the first row was for column titles, then the second row was for the first member data, then the third row which you specified in your code did not have any data in it yet, so when you filled in some data in that row, it created a member for that row.  That number '3' likely simply just needs to advance by +1 to 4, so that it is writing data to the next available row with no data in it, for it to create another member row.  If using that same rule multiple times in one Inventor session, you may be able to edit the rule to temporarily store the row number up into a SharedVariable, then when the rule runs again, it could be set-up to look for and get that row number from the SharedVariable, then add 1 to it to get the next row number to use.  This is not necessarily a 'smart' number, because it doesn't actually know how many rows there are in the table, but it might work in short run scenarios.  You would just need to start with an accurate number, then it will update the row number value each time it runs.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 10

e2000773
Enthusiast
Enthusiast

With that dynamic code I get Exception from HRESULT: 0x800A03EC error

0 Likes
Message 9 of 10

A.Acheson
Mentor
Mentor
Accepted solution

Hi @e2000773 

 

It looks like  the xlUp object was not  declared correctly when copied from VBA . 

This works in testing. 

 

Option Explicit On
AddReference "microsoft.office.interop.excel.dll"
Imports XL = Microsoft.Office.Interop.Excel

Dim oFactoryDoc As PartDocument
oFactoryDoc = ThisApplication.ActiveDocument

Dim oPartFactory As iPartFactory
oPartFactory = oFactoryDoc.ComponentDefinition.iPartFactory

Dim oWS As XL.Worksheet 
oWS = oPartFactory.ExcelWorkSheet

' Get Last Row 
Dim LastRow As Integer
LastRow = oWS.Cells(oWS.Rows.Count, 1).End(xl.XlDirection.xlUp).Rows.Row

MessageBox.Show("LastRowNumber: " & LastRow)

 

 

You can approach modifying the ipart directly in the table or through the excel sheet. This article shows two examples written in VBA. If you modify by table alone you can use handy built in methods like iPartTableRow.Copy.

It really sll depends on where and what you need to modify. The table is a little easier because you don't need excel reference to dll and extra complexity it brings. 

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 10 of 10

e2000773
Enthusiast
Enthusiast

OK now it works as I want. In my former code where I changed the row by number parameter (which I added to the code), the iPart table automatically updates in the model browser and now it only updates when I press save or go to edit table. what is different now?
This is what it was:

oWS.Cells(Number + 1, 1) = 

Now: 

oWS.Cells(LastRow + 1, 1) =

And I also removed this from a new code I had to make your version work, because it kept saying oRows is not declared:

oRows = oPartFactory.TableRows.Count + 1

Edit: seems to work now when I started fresh from empty

 

0 Likes