Ilogic code to do component replace based on file name search

Ilogic code to do component replace based on file name search

Wind_Talker
Contributor Contributor
2,847 Views
11 Replies
Message 1 of 12

Ilogic code to do component replace based on file name search

Wind_Talker
Contributor
Contributor
Hi All,
I am trying to create ilogic code in a top level GA which search given file name (which is created by combining string and inventor parameters ) in a given folder (folder exist on a network server and it is not the working directory).
If file exist in the folder then code should replace all instances of a component with that file and if file does not exist then just display a msg that file does not exist and ask user if they would like to do manual assembly of the component. If yes code should stop running and exit. If no code should re-run from start.

Any help is highly appreciated. I am using inventor 2015.

Please see my code below which i have come up with so far but it does not work.


Imports System
Imports System.IO
Imports Inventor

Public Class Bolt

Public Sub Main()

Dim FN As String
FN = "ISO 4014 - " & BOLT_DESIG & "ISO.ipt"
Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\"
Dim dirs As String
dirs = System.IO.Directory.GetFiles(SearchLocation, FN)
If dirs.Contains(FN) Then
Replace("BR BOLT", "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\ISO 4014 - " & BOLT_DESIG & "ISO.ipt", True)
Else
MsgBox.Show("The selected bolt size" & FN & "is not available in Meridian\Content Centre Files" _
& vbNewLine & "" _
& vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
& vbNewLine & "" _
& vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn",MsgBoxButtons.OK,MsgBoxIcon.Error)
End If
End Sub
End Class
0 Likes
Accepted solutions (1)
2,848 Views
11 Replies
Replies (11)
Message 2 of 12

wayne.brill
Collaborator
Collaborator

Hi,

 

Here is an update to your example that is replacing an occurrence on my system. 

 

Public Class Bolt

Public Sub Main()

'WB ADDED
Dim BOLT_DESIG As String = "0-80 UNF"

Dim FN As String
'WB commented
'FN = "ISO 4014 - " & BOLT_DESIG & "ISO.ipt"
'WB added
FN = "ANSI B18.6.3 - " & BOLT_DESIG & " - 0.375(34).ipt"
'WB commented
' Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\"
'WB added
Dim SearchLocation As String = "C:\Users\brillw\Documents\Inventor\WB_2-18-16\WB_Test\"

'WB added Try Catch block
Try
	Dim dirs() As String
	dirs = System.IO.Directory.GetFiles(SearchLocation, FN)
	
	Dim fullPath As String
	fullPath = dirs.getvalue(0)
	MessageBox.Show("Replacing occurrence with " & fullPath, "Title")

Catch
	MessageBox.Show("The selected bolt size" & FN & "is not available in Meridian\Content Centre Files" _
	& vbCrLf & "" _
	& vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
	& vbCrLf & "" _
	& vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn", MessageBoxButtons.OK,MessageBoxIcon.Error)
	Return
End Try
'WB commented
'Replace("BR BOLT", "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\ISO 4014 - " & BOLT_DESIG & "ISO.ipt", True)

' WB added, using a relative path and the name of the ipt
' see thw topic "Component Functions Reference (iLogic)" in the help file to see what this path is relative to
Dim strRelativeFileLocation As String = "\\WB_Test\" & FN
Component.Replace("Slot_Holes:1", strRelativeFileLocation, True)

'WB added
'NOTE if you want to use Contains then you need to add these statements
' to the top of the rule. An error occurs if these are not there:
'AddReference "System.Core"
'Imports System.Linq

'If dirs.Contains(fullPath) Then
'	Replace("BR BOLT", "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\ISO 4014 - " & BOLT_DESIG & "ISO.ipt", True)
'Else
'	MsgBox.Show("The selected bolt size" & FN & "is not available in Meridian\Content Centre Files" _
'	& vbNewLine & "" _
'	& vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
'	& vbNewLine & "" _
'	& vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn",MsgBoxButtons.OK,MsgBoxIcon.Error)
'End If
End Sub
End Class

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 12

Wind_Talker
Contributor
Contributor

Hi Wayne,

 

Many thnanks for your help. But I have few questions. Hope you would be happy to answer.

 

BOLT_DESIG parameter is calculated in another rule which is again a combination of two inventor parameters namely BOLT_SIZE and BLROUND. BOLT_SIZE is the dia of the bolt which can be anything from M10 to M36 and BLOUND is the length of the bolt which is dependent on the height of product the bolt is going into. BOLT_SIZE is user input through form while BLROUND is a calculated value. You can see that why BOLT_DESIG cannot be a fixed value and hence the part file name i.e. FN parameter. Is there anyway to use BOLT_DESIG parameter as it is in this rule rather than re-defining FN as a fixed value.

 

Also I tried your code but it still does not work for me. Please see the updated code below and also see the compilation errors I am getting. Sorry I am not very good with visual basic and API stuff.

 

Updated CODE:

 

Imports System
Imports System.IO 
Imports Inventor

Public Class Bolt

Public Sub Main()

Dim FN As String 
    FN = "ISO 4014 - M16 x 210ISO.ipt"
Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\"

Try
    Dim dirs As String
    dirs = System.IO.Directory.GetFiles(SearchLocation, FN)
    Dim fullPath As String
    fullPath = dirs.getvalue(0)
    
Catch
    MessageBox.Show("The selected bolt size" & FN & "is not available in Meridian\Content Centre Files" _
    & vbNewLine & "" _
    & vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
    & vbNewLine & "" _
    & vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn",MsgBoxButtons.OK,MsgBoxIcon.Error)
    Return
End Try

Dim strRelativeFileLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\" & FN
Component.Replace("BR BOLT", strRelativeFileLocation, True)

End Sub
End Class

 

Compilation Errors:

 

Error on Line 15 : Value of type '1-dimensional array of String' cannot be converted to 'String'.

Error on Line 17 : 'getvalue' is not a member of 'String'.

Error on Line 20 : 'MessageBox' is not declared. It may be inaccessible due to its protection level.

Error on Line 24 : 'MsgBoxButtons' is not declared. It may be inaccessible due to its protection level.

Error on Line 24 : 'MsgBoxIcon' is not declared. It may be inaccessible due to its protection level.

Error on Line 29 : 'Component' is not declared. It may be inaccessible due to its protection level.

 

 

 

Can you please have a look and suggest something.

 

Thanks

0 Likes
Message 4 of 12

Wind_Talker
Contributor
Contributor

Hi,

 

I think I have fixed the problems with my code. Now I don't get any compilation errors but the code is not doing anything. When I run the rule nothing happens. What am I missing?

 

Question is: Does the code actually runs or does it just skip from Public class to end class and ignore everything in between?

 

see updated code below. Any help is always appreciated.

 

 

Imports System
Imports System.IO 
Imports Inventor

Public Class Bolt

Sub Main(ByVal BOLT_DESIG As String)

Dim FN As String 
    FN = "ISO 4014 - " & BOLT_DESIG & "ISO.ipt"
Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\"

Try
    Dim dirs As String()
    dirs = Directory.GetFiles(SearchLocation, FN)
    Dim fullPath As String
    fullPath = dirs.getvalue(0)
     
Catch
    Dim msg as String
    msg = ("The selected bolt size" & FN & "is not available in Meridian\Content Centre Files" _
    & vbNewLine & "" _
    & vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
    & vbNewLine & "" _
    & vbCrLf & "Manually replace the bolt with desired length.")
    
    Dim title = "Bolt Replacement"
    
    Dim style = MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical
    
    MsgBox(msg, style, title)

    Return
End Try

Dim strRelativeFileLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\" & FN
Replace("BR BOLT", strRelativeFileLocation, True)

End Sub
End Class

  

0 Likes
Message 5 of 12

wayne.brill
Collaborator
Collaborator

Hi,

 

Do any of the MessageBox appear? With iLogic I use a MessageBox to try and determine where the code gets to. (add a couple at different places to see if it gets past a certain point. Like before and after this line:

Replace("BR BOLT", strRelativeFileLocation, True)

 

In my tests it had to have "Component" before Replace would work right.

Component.Replace( )

 

 

If this does not help you could attach an assembly with the rules and I could try to get it working.

 

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 12

Wind_Talker
Contributor
Contributor
Accepted solution

Hi Wayne,

 

I have finally got the code working but I don't understand why it is working now and how it is different from the previous version I posted. Only difference now is I have unchecked "Straight VB code" option.

 

Also for learning purpose, can you explain what this line is doing. It seems that without this line msg box under Catch command doesn't work.

Dim fullPath As String
    fullPath = dirs.getvalue(0)

 

 

See my updated code below.

Dim FN As String 
    FN = "ISO 4014 - " & BOLT_DESIG & "ISO.ipt"
Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\"

Try
    Dim dirs As String()
    dirs = System.IO.Directory.GetFiles(SearchLocation, FN)
    Dim fullPath As String
    fullPath = dirs.getvalue(0)
Catch
    MessageBox.Show("The selected bolt size " & FN & " is not available in Meridian\Content Centre Files" _
    & vbNewLine & "" _
    & vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _
    & vbNewLine & "" _
    & vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn",MessageBoxButtons.OK,MessageBoxIcon.Error)
    Return

End Try

Dim strRelativeFileLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\" & FN
Component.Replace("BR BOLT", strRelativeFileLocation, True)

 

0 Likes
Message 7 of 12

Anonymous
Not applicable

I am trying to implement this code into my model and I think it will solve a major roadblock that I have had for awhile. Can you elaborate on what some of it is doing so I can edit it to my needs. I have inserted questions below: Putting the threads together, I assume this is the final code used.

 

-------------------------------------------------------------------------------------------------------------

 

Imports System   Not sure what these first three lines do.
Imports System.IO
Imports Inventor

Public Class Bolt  What does this line reference

Sub Main(ByVal BOLT_DESIG As String) What does "Bolt_Desig" represent? Category folder?

Dim FN As String
    FN = "ISO 4014 - " & BOLT_DESIG & "ISO.ipt"  What is the ISO 4014, BOLT_DESIG, and ISO.ipt represent?
Dim SearchLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\" Is this just the content center category path?

 

Try

   Dim dirs As String()

   dirs = System.IO.Directory.GetFiles(SearchLocation, FN)

   Dim fullPath As String

   fullPath = dirs.getvalue(0)

Catch

   MessageBox.Show("The selected bolt size " & FN & " is not available in Meridian\Content Centre Files" _

   & vbNewLine & "" _

   & vbLf & "Please create a new content centre part file with correct bolt length and re-run the rules OR" _

   & vbNewLine & "" _

   & vbCrLf & "Manually replace the bolt with desired length.", "Bolt Replacemetn",MessageBoxButtons.OK,MessageBoxIcon.Error)

   Return

 

End Try

 

Dim strRelativeFileLocation As String = "\\BG-DOC1\am\Vault,D-Comtec\CONTENT CENTRE FILES\en-US\ISO 4014(2)\" & FN  Why use this "stRelativeFileLocation" because it seems that this the component.replace could use SearchLocation instead and this is irrelevant?

Component.Replace("BR BOLT", strRelativeFileLocation, True)  



End Sub
End Class

 

 

 

---------------------------------------------------------------------------------------------------------------------------------

 

 

 

Thanks for your help!!

0 Likes
Message 8 of 12

Wind_Talker
Contributor
Contributor

Hi Caleb,

 

Thanks for your questions. I am sorry I am busy in training at the moment, so can't answer your questions right now. But give me couple of days and I will get back to you.

 

Regards

 

Wind_Talker

 

0 Likes
Message 9 of 12

Wind_Talker
Contributor
Contributor

Hi Caleb,

 

your answers are as follows in sequence.

 

1. This command imports windows system functionality into inventor. This is a VB command. You don't necessarily need that.

 

2. I was trying to create a VB class module here. Again you can ignore it. Bolt is the name of the class.

 

3. This is an inventor parameter which defines the size of bolt in my code. e.g M10 x 50

 

4. Here i am combining fixed part with the dynamic part to create the full name of the part file i.e. model of the bolt to use. ISO 4014 is a thread class standard. ISO.ipt is just the last name of the file. So, FN gives you the full name of the part file. e.g. ISO 4014 - M10 x 50ISO.ipt is the full name of one of the model file for bolt.

 

5. This is the path to the folder where I want the code to search for the bolt part file defined in step 4 above. This path can be whatever you want but should be a valid path. This can be on your own computer or a network path.

 

6. Please ignore that portion as this is not the latest code. This section was a trial and didn't quite do what I wanted to do. Please have a go through the following post by me where it will show the full code I used.

 

http://forums.autodesk.com/t5/inventor-customization/comp-replace-by-next-or-previous-file-in-a-netw...

 

Hope that helps. If you need further support, let me know.

 

Regards

 

Wind Talker

 

 

Message 10 of 12

Anonymous
Not applicable

I have one more question on extending this code that you may have some good insight on. I created a thread for it here.

 

ParameterChangeSearchFilesForExistingWithSameParameter

 

Essentially, I want to create a code for my "master parts" that searches folder for similar part with a slight change in user parameter. In each part, there is a range from 1 to 6 user parameters that I will create. These include length, height, radius, ext...

 

If I went a route where I grabbed a parameter and had the user select any radius they wanted, I would have to create new part numbers for each of the parts that changed radius. However, after maybe 2 months of this, the vault would get cluttered with duplicate parts.

 

Its a fairly large problem that I figured you may have some good approach for. Also, I generally prefer to stay away from iParts.

 

0 Likes
Message 11 of 12

Wind_Talker
Contributor
Contributor

Hi Caleb,

 

I had a go through your thread. Can you please answer the following for my better understanding of the problem.

 

1. How the radius parameter is defined on user form? Is it a drop down list with some fixed values or is it a text box entry where user can type whatever value they want.

 

2. Please define naming procedure or criteria of your part file. What portion of name is dynamic i.e. change when going from one file to another in a folder.

 

3. Are there any other parameters except the radius which will affect the name of the file.

 

4. How the name of the part is defined in the assy model tree. Is it Inventor generated name or a user defined name.

 

In general this should be straight forward and fairly easy to code.

 

Regards

 

Wind_Talker

0 Likes
Message 12 of 12

Anonymous
Not applicable

1. How the radius parameter is defined on user form? Is it a drop down list with some fixed values or is it a text box entry where user can type whatever value they want.

The radius parameter is hopefully variable where the user can type whatever value they want. Because we are constantly customizing parts, it is easier for us to not have to populate the dropdown box constantly.

 

2. Please define naming procedure or criteria of your part file. What portion of name is dynamic i.e. change when going from one file to another in a folder.

I want to be able to completely rename the part. We are changing to a solely numerical naming system that generates a new number for us. This number will need to be manually inputted as the file name for new parts. Ideally a prompt to ask user to "create new part number and place it here."

 

 

3. Are there any other parameters except the radius which will affect the name of the file.

The file name is simply a generated number.

 

 

4. How the name of the part is defined in the assy model tree. Is it Inventor generated name or a user defined name.

I am not sure if I fully understand this question, but I suspect you were asking me about the assembly model tree.

I am renaming each of the Assembly Model Names in the model tree to something a little more recognizable which allows me to use a component.replace of a similar part.

 

Currently, as my basic texting model, I have a pressure vessel with some Saddles that match the outside radius of the pressure vessel. I have a form that allows the user to select the width of the saddle and the length and radius of the tank. Then the code should search for an existing vessel in vault with those parameters (length and radius) as well as the parameters of the saddle (radius and width). If the parts with these parameters exist then do component.replace of the correct parts, if non-exist, then prompt user to do a SaveCopyAs of the newly created part.

 

 

 

 

0 Likes