How do I run an external rule on all files in a folder?

How do I run an external rule on all files in a folder?

Anonymous
Not applicable
4,347 Views
16 Replies
Message 1 of 17

How do I run an external rule on all files in a folder?

Anonymous
Not applicable

I have written an external rule that I need to run on ~3,000 files in a folder in a shared network location. I have nearly been successful using the following code (Batch Update)

 

Dim Folder As New IO.DirectoryInfo("folder with idw files to be processed")
Dim FileList As New List(Of String)
For Each File As IO.FileInfo In Folder.GetFiles("*.idw",IO.SearchOption.AllDirectories)
If File.FullName.Contains("OldVersions") = False Then
FileList.Add(File.FullName)
End If
Next

i = MessageBox.Show(FileList.Count & " files were found", "File count", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)



Dim oDoc1 As Document
Dim DocName As String
For Each DocName In FileList
oDoc1 = ThisApplication.Documents.Open(DocName, True)

MessageBox.Show(DocName, "Path and file name", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
iLogicVb.RunExternalRule("Batch - Update title block and border")
ThisDoc.Save
oDoc1.Close(False)
Next

 

This initially seemed to work, but for some reason, when each document is opened, iLogic cannot 'see' the file - the "Path and file name" message box returns nothing. As a result, the "Batch - Update title block and border" rule doesn't run as expected.

 

When the "Batch - Update title block and border" rule is manually run on a drawing, it works as intended, but whe called from the "Batch Update" rule it doesn't. When the "Batch - Update title block and border" rule gets to

Dim Sheet_Size As String = ActiveSheet.Size 

 instead of setting Sheet_Size to equal A2/A3/A4/custom, it sets it to equal the size of the active page in the drawing the original "Batch Update" rule was run from. 

 

How do fix this?

0 Likes
Accepted solutions (2)
4,348 Views
16 Replies
Replies (16)
Message 2 of 17

cadman777
Advisor
Advisor

CodeInjector on GitHub

 

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 3 of 17

WCrihfield
Mentor
Mentor

I made a couple of minor adjustments to your posted code, that may, or may not help some.

Dim Folder As New IO.DirectoryInfo("folder with idw files to be processed")
Dim FileList As New List(Of String)
For Each File As IO.FileInfo In Folder.GetFiles("*.idw",IO.SearchOption.AllDirectories)
	If File.FullName.Contains("OldVersions") = False Then
		FileList.Add(File.FullName)
	End If
Next

i = MessageBox.Show(FileList.Count & " files were found", "File count", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

For Each DocName As String In FileList
	Dim oDoc1 As Document = ThisApplication.Documents.Open(DocName, True)
	MessageBox.Show(DocName, "Path and file name", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	iLogicVb.Automation.RunExternalRule(oDoc1, "Batch - Update title block and border")
	oDoc1.Save2(False)
	oDoc1.Close(True)
Next

  But I would need to see the contents of the other rule in order to attempt to troubleshoot any potential issues that may be going on there.  Usually, in these types of situations, it has to do with how you specify which documents the code is supposed to be targeting (acting upon) that causes problems when running them remotely.  They may function just fine when there is just one document open and active, but when attempting to run it remotely, while another document may be open or active, you can start to run into reference problems.  Here is a contribution post I created some time past that goes into much more detail on the subject of document references.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 17

Anonymous
Not applicable

Thanks for the link, I'll have a read and see if this helps.

 

The code within "Batch - Update title block and border" is as follows

 

Sub Main()
ThisDrawing.ResourceFileName = "\\pccox\DRAWINGS\Inventor\templates\SHEET REFORMAT MASTER.idw" 'document to copy title block and border from
Dim oDrawingDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oDrawing As DrawingDocument 'variable
oDrawing = ThisApplication.ActiveDocument 'variable
Sheet_Count = oDrawing.Sheets.Count 'variable
oDoc = ThisDoc.Document
Current_Sheet = oDoc.ActiveSheet.Name
Resize = "No"
oDrawingDoc.Activate

For SheetNumber = 1 To Sheet_Count 'for loop to run through each sheet in drawing
	oDrawingDoc.Sheets(SheetNumber).Activate 'activates sheet
	Sheet_Size = ActiveSheet.Size 'sets Sheet_size to equal the size of the active sheet (A2,A3,custom)
	ThisApplication.UserInterfaceManager.DoEvents	

	If Sheet_Size = "A2"'checks if the sheet size is a custom size
		Resize = "No"
		i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A2 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

	Else If Sheet_Size = "A3"'checks if the sheet size is a custom size
		Resize = "No"
		i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A3 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Else 
		Resize = "Yes"
		ThisApplication.ActiveDocument.ActiveSheet.Orientation  = kLandscapePageOrientation
		ActiveSheet.ChangeSize("A3", moveBorderItems := True) 'sets sheet to A3 - old template was 400x300mm, close to A3
		Sheet_Size = ActiveSheet.Size
		i = MessageBox.Show("Page resized!", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In custom branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

	End If
	

	ThisApplication.ActiveDocument.ActiveSheet.Orientation  = kLandscapePageOrientation		
	Sheet_Size = ActiveSheet.Size 'sets Sheet_size to equal the size of the active sheet (A2,A3)


	ActiveSheet.SetTitleBlock(Sheet_Size, "PLACEHOLDER - CHANGE ME", "PLACEHOLDER - CHANGE ME")'applies title block that corresponds to page size
	ActiveSheet.Border = Sheet_Size & " Border"'applies border to corresponding page size
	
Next SheetNumber'loops the above until all pages have been processed

 It all runs as expected when run manually in a drawing, but when called from the "Batch Update" rule above, the "Sheet_Size = ActiveSheet.Size" sets Sheet_Size to equal the size of the sheet that was active when "Batch Update" was run, instead of the active sheet in the drawing that has been opened

0 Likes
Message 5 of 17

tobias.orlow
Alumni
Alumni

Hi @Anonymous ,

you might also want to have a look at the following app within the App Store to automate the process of executing iLogic rules in multiple files in batch: iLogic Rule Batch Tool | Inventor | Autodesk App Store

 

Many thanks,

Tobias

Tobias Orlow
Designated Support Specialist
Customer Success Organization
Linkedin: www.linkedin.com/in/tobiasorlow/
0 Likes
Message 6 of 17

Anonymous
Not applicable

Hi @tobias.orlow, thanks for the link. Unfortunately the same issue happens with the iLogic Rule Batch tool as with my "Batch Update" external rule.

 

I need a way to use the 

Sheet_Size = oDrawingDoc.ActiveSheet.Size

operation to look at the drawing that has been opened, instead of the drawing that the "Batch Update" rule was run from

0 Likes
Message 7 of 17

cadman777
Advisor
Advisor

Hey Tobias,

I looked at the app on the link you provided.

It looks a lot like MegaJerk's 'CodeInjector'.

Did you guys based your app on his?

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 8 of 17

tobias.orlow
Alumni
Alumni

Hi @cadman777 ,

 

while some of the functionality is similar, the app is not based on the CodeInjector project in any way.

I found it would be helpful to provide an up-to-date tool to achieve such automations as they are valuable and the mentioned project is not maintained anymore.

 

Many thanks, 

Tobias Orlow
Designated Support Specialist
Customer Success Organization
Linkedin: www.linkedin.com/in/tobiasorlow/
0 Likes
Message 9 of 17

A.Acheson
Mentor
Mentor

You will need to make the drawing opened available for editing so you can use 

Dim oDrawingDoc As DrawingDocument = ThisApplication.ActiveEditDocument

The alternative would be to pass the full file path to the rule but that is more complicated for sure.  

 

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

WCrihfield
Mentor
Mentor

There are lots of potential for problems in that other code that I'm seeing that could definately be causing trouble when ran remotely like this.  It's going to take some time to comb down through it all and attempt to code some things differently, while maintaining its functionality.  Right from the start, it is creating 3 different variables for the document it is supposed to be working with, which I definitely don't recommend.  Then the repeated use of the shortcut term ActiveSheet, will need to be replaced with the equivalent object obtained from the document object, the longer way, because ActiveSheet wants to default to the 'local' document first, then to whichever document was active when the rule first started.  I don't mention this one in my references post, but I believe it is defined the same way as other similar reference terms are.  I believe the 'active' document reference may work for us in this situation, because your top rule is opening each drawing visibly, before attempting to run the rule, which should (theoretically) make that newly opened drawing document the 'active' document.  But since this is a batch run situation, I'm not sure if there may be a timing issue going on or not.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 17

WCrihfield
Mentor
Mentor

Well...I honestly don't recall how to do some of these things the 'long way', because I just never had to before.  First, I don't remember (or maybe never knew) how to set the 'ResourceFileName' data without using that document reference 'ThisDrawing', but I believe we need to do it somehow using our main/only document variable, instead of that reference.  Second, I don't recall having to re-associate things which might have been attached to the Border of a drawing to the new Border after changing the border.  This functionality is part of the ActiveSheet.ChangeSize  (link1, link2) method.  I know how to change the sheet's size, just not the re-associate stuff part.

 

Here's what I've got so far, but I'm not sure I've still got everything set up as you need it, so you may have to test it on a single drawing to start with, before attempting to run it in your batch process.

Sub Main()
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	'I can't remember how to do this the 'long way'
	'oDDoc.ReferencedOLEFileDescriptors.Add("\\pccox\DRAWINGS\Inventor\templates\SHEET REFORMAT MASTER.idw", OLEDocumentTypeEnum.kOLEDocumentLinkObject)
	ThisDrawing.ResourceFileName = "\\pccox\DRAWINGS\Inventor\templates\SHEET REFORMAT MASTER.idw" 'document to copy title block and border from
	Current_Sheet = oDDoc.ActiveSheet.Name
	Resize = "No"
	'oDDoc.Activate 'it is already active, because of how we retrieved it

	For Each oSheet As Sheet In oDDoc.Sheets
		oSheet.Activate 'activates sheet
		Sheet_Size = oSheet.Size 'sets Sheet_size to equal the size of the active sheet (A2,A3,custom)
		ThisApplication.UserInterfaceManager.DoEvents	

		If Sheet_Size = DrawingSheetSizeEnum.kA2DrawingSheetSize Then
			Resize = "No"
			MsgBox("Sheet size is " & Sheet_Size.ToString & vbCrLf & "In A2 branch", vbOKOnly + vbCritical, "Page size")
		ElseIf Sheet_Size = DrawingSheetSizeEnum.kA3DrawingSheetSize Then
			Resize = "No"
			MsgBox("Sheet size is " & Sheet_Size.ToString & vbCrLf & "In A3 branch", vbOKOnly + vbCritical, "Page size")
		Else 
			Resize = "Yes"
			oSheet.Orientation = kLandscapePageOrientation
			oSheet.Size = DrawingSheetSizeEnum.kA3DrawingSheetSize
			'need code here to making sure border items get moved as originally planned
			'ActiveSheet.ChangeSize("A3", moveBorderItems := True) 'sets sheet to A3 - old template was 400x300mm, close to A3
			Sheet_Size = oSheet.Size
			MsgBox("Page resized!", vbOKOnly + vbCritical, "My iLogic Dialog")
			MsgBox("Sheet size is " & Sheet_Size.ToString & vbCrLf & "In custom branch", vbOKOnly + vbCritical, "Page size")
		End If

		oSheet.Orientation  = kLandscapePageOrientation		
		Sheet_Size = oSheet.Size 'sets Sheet_size to equal the size of the active sheet (A2,A3)
		
		Dim oTBDef As TitleBlockDefinition
		Dim oBDef As BorderDefinition
		If oSheet.Size = DrawingSheetSizeEnum.kA2DrawingSheetSize Then
			oTBDef = oDDoc.TitleBlockDefinitions.Item("A2")
			oBDef = oDDoc.BorderDefinitions.Item("A2 Border")
		ElseIf oSheet.Size = DrawingSheetSizeEnum.kA3DrawingSheetSize Then
			oTBDef = oDDoc.TitleBlockDefinitions.Item("A3")
			oBDef = oDDoc.BorderDefinitions.Item("A3 Border")
		End If
		oSheet.AddTitleBlock(oTBDef)
		oSheet.Border.Delete
		oSheet.AddBorder(oBDef)
	Next 'loops the above until all pages have been processed
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 17

cadman777
Advisor
Advisor

OK, thanx for the update.

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 13 of 17

WCrihfield
Mentor
Mentor

Hi @Anonymous.  Here is one way (work-around) to avoid using the 'ThisDrawing.ResourceFileName' tool, that will still allow you to pull the needed resources from that template drawing file if needed.  It basically just opens that template drawing document 'invisibly' (in the background, without a tab), then extracts the title block definitions and border definitions from that document to two variables we can use later in the code.  Then later, when we don't need those resources anymore, closes that template file.  Opening and closing that template file shouldn't use much resources, since it is only being opened invisibly.

 

Dim oTemplatePath As String = "\\DIV29-FS-01\Shared\Engineering\Templates\A-Size Standard Drawing.idw"
Dim oTemplateDDoc As DrawingDocument = ThisApplication.Documents.Open(oTemplatePath, False) 'False = invisible
Dim oTemplateTBDefs As TitleBlockDefinitions = oTemplateDDoc.TitleBlockDefinitions
Dim oTemplateBDefs As BorderDefinitions = oTemplateDDoc.BorderDefinitions
'...your other code which pulls any needed resources from these sources...
'then close that template drawing document
oTemplateDDoc.Close(True) 'True = skip any possible attempts to save the Template when closing it

 

If you replaced that line of code with this block of code, then you would just need to use these two new variables down within your code, whenever you need to specify a title block or border to use, after a sheet size change.

 

You might also benefit from using a Boolean type variable as a flag, initially set to False. Then when looping through the sheets, if you had to change the sheet's size, set this variable to True. Then when it comes to the part of the code where you attempt to change the sheet's border and title block, you could check this variable.  If it is still False, then it won't need to attempt to change its border and title block.

 

Another thought would be to open this template document within the top (run the other rule) rule code, then just pass these two resources to the other rule that it is running, through the NameValueMap & RuleArguments route, so that you would only be opening and closing it one time for the whole batch run process.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 17

Anonymous
Not applicable
Accepted solution

So I got it working, with an irritatingly small change required

iLogicVb.RunExternalRule("Batch - Update title block and border")

Now reads

oDoc1 = ThisApplication.Documents.OpenWithOptions(DocName, oOptions, True)
iLogicAuto.RunExternalRuleWithArguments(oDoc1, "Batch - Update title block and border", oArgs)

Where DocName is a string containing the full filepath of the drawing to be updated

 

I'm pretty sure the issue was the original call to run the "Batch - Updater title block and border" iLogic rule runs the rule inside the document which the first rule was run inside, which causes issues with ActiveSheet and ActiveDocument, where as the new call for for the iLogic rule tells inventor to run the rule inside a specific document, in this case DocName

 

0 Likes
Message 15 of 17

Anonymous
Not applicable
Accepted solution

Full code now reads:

 

To replace title block and border, and place sketch symbols for all drawings in a folder

 

Batch Update

 

Sub Main()
'	On Error GoTo errtrap
	
	'setup to read in the log file
    Dim oFile As String = "Text file for report.txt"'must already exist
    Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oFile)
    Dim DoneList As String
    Dim line As String
    
    'read in log file
    Do
        line = oReader.ReadLine()
        DoneList = DoneList & line
    Loop Until line Is Nothing
    
    'close log for reader
    oReader.Close()
	
	Dim Folder As New IO.DirectoryInfo("Folder location containing all drawings to be updated")'Folder in which drawings to be updated are located
	Dim FileList As New List(Of String)'creates a list of files
	For Each File As IO.FileInfo In Folder.GetFiles("*.idw", IO.SearchOption.AllDirectories)'runs below "If" loop on every file with ".idw" file extension
		If File.FullName.Contains("OldVersions") = False Then'exludes all files that have "OldVersions" in their filepath from the next line
			'to exclude any files with a specific string of characters from being processed, remove the ' from the next 5 lines & change "Your Text here" to you character string
		'	If File.FullName.Contains("Your Text here") = False Then 
		'		If File.FullName.Contains("OldVersions") = False Then 
		'			FileList.Add(File.FullName)
		'		End If
		'	End if
			FileList.Add(File.FullName)'adds file to the list to be processed
		End If
	Next
	
	'display files found by search, exit if 0
    If FileList.Count > 0 Then
        MsgBox(FileList.Count & " files were found")
    Else
        MsgBox("No more files were found.  ")
        Exit Sub
    End If
	
	'Shows how many files have been found and will be processed - currently approx 2m30s per file
	'MessageBox.Show(FileList.Count & " files were found", "File count", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

	Dim oDoc1 As Document'variable
	Dim oAppend As System.IO.StreamWriter
	 oAppend = IO.File.AppendText(oFile)
'	Try
 '      
  '  Catch
   '     GoTo Close
    'End Try
	
	Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	'change these to false if you need to open and update from model.  
	oOptions.Value("DeferUpdates") = True
	oOptions.Value("FastOpen") = True
	
	For Each DocName As String In FileList'iterates through every file in the filelist
		Try
		'oDoc1 = ThisApplication.Documents.Open(DocName, True)'opens file
		oDoc1 = ThisApplication.Documents.OpenWithOptions(DocName, oOptions, True)
		Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument'variable to allow iteration through all sheets in each drawing
		ThisApplication.UserInterfaceManager.DoEvents'wait until all processes have finished
		'Dim Path As String = ThisDoc.PathAndFileName(False)'gets the path of the active document
		'Dim oDrawing As Inventor.DrawingDocument = ThisApplication.ActiveDocument'variable
		Sheet_Count = oDoc1.Sheets.Count 'variable - counts number of sheets in opened drawing
		Dim SheetNumber As Integer = 1'sets sheetnumber variable to equal 1 so below external rule is run starting on sheet 1 - passed through to "Batch - Update title block and border" rule
		For Each oSheet As Sheet In oDDoc.Sheets'iterates through every sheet in the open drawing
			ThisApplication.UserInterfaceManager.DoEvents'wait until all processes have finished
			Dim iLogicAuto = iLogicVb.Automation'sets up iLogic Automation
			Dim oArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap'creates set of arguments to be passed to external rule
			oArgs.Add("SheetNumber", SheetNumber)'adds SheetNumber variable to argumetns to be passed through
			iLogicAuto.RunExternalRuleWithArguments(oDoc1, "Batch - Update title block and border", oArgs)'call for "Batch - Update title block and border" to be run from oDoc1 and passes through oArgs
			SheetNumber = SheetNumber + 1'increments SheetNumber by one to active next sheet in next loop
		Next 'loops the above until all pages have been processed
		'record that the changes were made
        oAppend.WriteLine(DateTime.Now.ToString("s") & DocName) 
		
		oDoc1.Save2(False)'saves drawing
		oDoc1.Close(True)'closes drawing
		'nextfile :
		Catch
			oAppend.WriteLine(DateTime.Now.ToString("s") & DocName & "              < an error occured")
		oDoc1.Close(True)
	End Try
	Next'moves to the next file in filelist
	
	'close the writer when done
    close:
	oAppend.Close()
	MessageBox.Show("Done! " & FileList.Count & " files processed. " & vbCrLf & "See " & oFile & " for log", "Drawing update complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	'MessageBox.Show("Done!" & FileList.Count & " files were processed" & +vbCrLf+ & "See " & oFile " for log", "Rule complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	Exit Sub
	
	
	
'	errtrap:
'	Select Case Err.Number
'	Case Else
'		oAppend.WriteLine(DateTime.Now.ToString("s") & DocName & "              < an error occured")
'		oDoc1.Close(True)
'		
'		Resume nextfile
'	End Select
'	
		
		
	
End Sub

 

 

Batch - Update Title Block and Border

 

Sub Main()
	Dim SheetNumber As Integer = RuleArguments("SheetNumber")
	'MessageBox.Show(SheetNumber, "SheetNumber Value", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	Resize(SheetNumber)
	UpdateStyles()
	ChangeStyle()
	ChangeBorder()
	ChangeTitleBlock()
	AddOADimsTable(SheetNumber)
	AddRevTable()
End Sub

Sub Resize(SheetNumber)
	Dim oDrawingDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
	Dim oDrawing As DrawingDocument = ThisApplication.ActiveDocument 'variable
	Sheet_Count = oDrawing.Sheets.Count 'variable
	Sheet_Size = ActiveSheet.Size
	'MessageBox.Show("1", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	oDrawingDoc.Sheets(SheetNumber).Activate 'activates sheet
	'MessageBox.Show("2", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
	ThisApplication.UserInterfaceManager.DoEvents	
	'i = MessageBox.Show(Sheet_Size, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	If Sheet_Size = "A2"'checks sheet size
		'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A2 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

	Else If Sheet_Size = "A3"'
		'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A3 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		
	Else If Sheet_Size = "A4"
		ThisApplication.ActiveDocument.ActiveSheet.Orientation  = kLandscapePageOrientation
		ActiveSheet.ChangeSize("A3", moveBorderItems := True) '
		Sheet_Size = ActiveSheet.Size
		'i = MessageBox.Show("Page resized!", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A4 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Else 
		'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In custom branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		ThisApplication.ActiveDocument.ActiveSheet.Orientation = kLandscapePageOrientation
		ActiveSheet.ChangeSize("A2", moveBorderItems := True) 'sets sheet to A2
		Sheet_Size = ActiveSheet.Size
		'i = MessageBox.Show("Page resized!", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In custom branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	End If
End Sub

Sub UpdateStyles()
    OpenDoc = ThisDoc.Document
    oStyles = OpenDoc.StylesManager.Styles
    For Each oStyle As Style In oStyles
        If Not oStyle.UpToDate Then
            oStyle.UpdateFromGlobal
        End If 
    Next
End Sub

Sub ChangeStyle()
	
	Dim oDrawingDoc As DrawingDocument = ThisDoc.Document
		'the styles collection
	Dim oStandardDesignStyles As DrawingStandardStylesEnumerator = oDrawingDoc.StylesManager.StandardStyles

	'find the right style and set it then exit
	Dim oStandardStyle As DrawingStandardStyle
	
	
	For Each oStandardStyle In oStandardDesignStyles
	    If oStandardStyle.Name = "Style Name here" Then
	        oDrawingDoc.StylesManager.ActiveStandardStyle = oStandardStyle
	        Exit Sub
	    End If
	Next
End Sub

Sub ChangeBorder()
	ThisDrawing.ResourceFileName = "Full filepath of template drawing with Border.idw" 'document to copy title block and border from
	Sheet_Size = ActiveSheet.Size
	ActiveSheet.Border = "Border name here"'applies border to corresponding page size
End Sub

Sub ChangeTitleBlock()
	ThisDrawing.ResourceFileName = "Full filepath of template drawing with title blocks.idw" 'document to copy title block and border from
	Sheet_Size = ActiveSheet.Size
	ActiveSheet.SetTitleBlock("Title block name here", "PLACEHOLDER - CHANGE ME", "PLACEHOLDER - CHANGE ME")'applies title block that corresponds to page size
End Sub

Sub AddOADimsTable(SheetNumber)
	Dim oDrawingDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
	Sheet_Count = oDrawingDoc.Sheets.Count 'variable
	oSymbolDrawingName = "Sketch symbols.idw"

	'This is the name of the symbol to place
	oSymbolName = "OVERALL DIMENSION TABLE"

	Dim oSymbol As SketchedSymbol
	Sheet_Size = ActiveSheet.Size 'gets active sheet size
	Dim oPosition As Point2d

	'define the drawing document
	oDrawingDoc = ThisApplication.ActiveDocument

	Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
	oSketchSymLib = oDrawingDoc.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item(oSymbolDrawingName)
	Dim oSymDef As SketchedSymbolDefinition

	'place the symbol in the Drawing Resources folder
	oSymDef = oDrawingDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, oSymbolName, True)

	oDrawingDoc.Sheets(SheetNumber).Activate 'activates sheet
	Sheet_Size = ActiveSheet.Size
	If Sheet_Size = "A2"
			oPosition= ThisApplication.TransientGeometry.CreatePoint2d(45.65, 2.5)
			oSymbol = oDrawingDoc.ActiveSheet.SketchedSymbols.Add(oSymDef, oPosition, 0, 1, )
		Else If Sheet_Size = "A3"
			oPosition= ThisApplication.TransientGeometry.CreatePoint2d(28.25, 2.5)
			oSymbol = oDrawingDoc.ActiveSheet.SketchedSymbols.Add(oSymDef, oPosition, 0, 1, )
		Else
			'i = MessageBox.Show("Page size should be A2 or A3", "Incorrect page size!", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	End If
End Sub

Sub AddRevTable()
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim Sheet_Size
	Dim X_Pos
	Dim Y_Pos

	Dim oRTBs As RevisionTables = oDrawDoc.ActiveSheet.RevisionTables

	Sheet_Size = ActiveSheet.Size

	If Sheet_Size = "A2"
			X_Pos = 47.4
			Y_Pos = 41
		Else If Sheet_Size = "A3"
			X_Pos = 30
			Y_Pos = 28.7
	End If
		
	Dim oLocation As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos, Y_Pos)

	'add a new table
	Dim oRTB As RevisionTable
	oRTB = oRTBs.Add2(oLocation, True, True, False, "00")
End Sub

 

0 Likes
Message 16 of 17

Kassenbouw
Enthusiast
Enthusiast

Hi,

 

I'm trying to use your code for running a rule on a folder but I don't know where to change the folder. It seems IO.DirectoryInfo is a link to Inventor2021\Bin\Folder. How do I change this?

 

Thanks

0 Likes
Message 17 of 17

WCrihfield
Mentor
Mentor

Hi @Kassenbouw.  That object is not specific to any folder until you specify which folder it is to represent.  Basically, you would supply the path of the folder you want to work with within the () area.  Here is the official online documentation page for that Class:  (you may need to change the drop-down from C# to VB at the top of the page)

https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo?view=net-5.0 

 

And here is the official online documentation page for the specific variation of the GetFiles method he used above.

https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getfiles?view=net-5.0#System_IO_... 

 

These are not the only objects and methods that can be used for this (and similar) processes, but if they meet your specific needs, then that's all that matters.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes