Update IDW with each model state and save as separate PDF

Update IDW with each model state and save as separate PDF

insomnix
Advocate Advocate
637 Views
7 Replies
Message 1 of 8

Update IDW with each model state and save as separate PDF

insomnix
Advocate
Advocate

I am attempting to update some code I have used over the years to transition away from iFactories. The iLogic code below is currently located in an assembly file, though I'm trying to write it in a way that it will work in a parts file as well. So far it will iterate through each model state. When it comes to a value of true on the generate field*, it will execute the code to generate a PDF.

 

GeneratePrint mostly works. It will open the IDW and export as a PDF. However, the model state on the IDW does not change to whatever the current active model state is. It must be updated manually or through code. I have tried updating the IDW view from the assembly iLogic code, and I've tried to update it from the IDW. Neither is working very well for me.

 

So, how do I change the model state on the IDW based on the active model state in the assembly/parts file through iLogic?

 

Assembly code:

Sub Main()
	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	    
	Dim oDef As ComponentDefinition
	oDef = oDoc.ComponentDefinition
	
	Dim oStates As ModelStates 	
	oStates = oDef.ModelStates

	Dim oTable As ModelStateTable	
	oTable = oStates.ModelStateTable

	Dim oRow As ModelStateTableRow 
	For Each oRow In oTable.TableRows
		ThisDoc.ActiveModelState = oRow.MemberName
		
		If iProperties.Value("Custom", "Generate") = True Then
		    'MessageBox.Show(oRow.MemberName)
			GeneratePrint(iProperties.Value("Custom", "DWGTemplate"),iProperties.Value("Custom", "DWGNumber"), oRow.MemberName)
		End If
		
	Next
End Sub


Sub GeneratePrint(dwg_template As String, dwg_number As String, model_state_fin As String)
	Dim oDoc As DrawingDocument
	dwgFile = ThisDoc.Path & "\" & dwg_template & ".idw"
	
	'this will prevent faded images when generating pdf files
	ThisApplication.DrawingOptions.EnableBackgroundUpdates = False
'	'speed up the process
'	ThisApplication.ScreenUpdating = False

	'get PDF target folder path
	oFolder = ThisDoc.Path & "\PDF"
	pdfFile = oFolder & "\rsi" & dwg_number & ".pdf"
	
	'Check for the PDF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If

	'RuleParametersOutput()
	iLogicVb.UpdateWhenDone = True
	InventorVb.DocumentUpdate()
	ThisDoc.Save
	
	oDoc = ThisApplication.Documents.Open(dwgFile)

'	ThisApplication.ScreenUpdating = True

	oDoc.SaveAs(pdfFile, True)
	oDoc.Close(True)

	'turn this back on, it will speed up regular use.
	ThisApplication.DrawingOptions.EnableBackgroundUpdates = True
End Sub

 

IDW code:

Sub Main()
	Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim width As Double
	Dim length As Double
	Dim oScale As Double
	
	
	'get current active sheet
	Dim oSheet As Sheet
	oSheet = oDoc.ActiveSheet

	Dim oAssyDoc As AssemblyDocument
	ModelStateFile = ThisDoc.Path & "\" & "R39225-MD5-FIN" & ".iam"
	'oAssyDoc = ThisApplication.Documents.Open(ModelStateFile,False)

	'ActiveSheet.View("FRONT").View.SetActiveModelState(oAssyDoc.ActiveModelState)

	width = Parameter("R39225-MD5-FIN.iam.width")
	length = Parameter("R39225-MD5-FIN.iam.length")

	oScale = Round(Min(Min(ActiveSheet.Height / 2.5 / length, ActiveSheet.Width / 4.1 / width), .18), 2)
	ActiveSheet.View("FRONT").Scale = oScale
	ActiveSheet.View("BACK").Scale = oScale
	ActiveSheet.View("ISO").Scale = oScale

	'set active sheet back to original
	oSheet.Activate	
End Sub

 

*I had to use a custom iProperty to reference the value. I tried to find a way to read directly from the table row, but could not find a way to do this. If anyone knows how to read a cell value instead of having to place the value in an iProperty, that would be welcome information.

0 Likes
638 Views
7 Replies
Replies (7)
Message 2 of 8

insomnix
Advocate
Advocate

I've attached two files, an ipt and idw. They are smaller than the assembly I was working on in the previous example. At this point everything works as expected, except how do I update the model state on the IDW?

0 Likes
Message 3 of 8

A.Acheson
Mentor
Mentor

I haven't got to use model states yet but my assumption is that it is very similar to iparts. Here is a link to ipart workflow. 

This is the business portion of ipart view change for member. 

*********

For i = 1 To iPartF.TableRows.Count - 1

Set oPartDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument

Dim sPath As String
sPath = Replace(oPartDoc.FullFileName, iPartF.FileNameColumn(i).Value, iPartF.FileNameColumn(i + 1).Value)

Call iPartF.CreateMember(i + 1)

Call oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference(sPath)

 

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

insomnix
Advocate
Advocate

Model states are a bit different. 

 

In the idw I created a test rule and entered the below line, which switched the model state on the drawing. However, I cant figure out how I can use this line to assign the model state to the idw view from the iam or ipt. If you look at the ipt, I am already iterating through each model in the generate rule.

ActiveSheet.View("FRONT").View.SetActiveModelState("U420521A")

 

Alternatively, I can't work out how to iterate through each model state from the IDW. The only reason I need to iterate through the model state is so I can capture the width and length parameters from the ipt to scale the drawing properly. 

0 Likes
Message 5 of 8

insomnix
Advocate
Advocate

OK, forward movement. I found the line I need to change the model state in the view. Still some work to do here. There were some odd results with the part number field in the title block, like it wasn't updating with the model state change. I may just need to add a line to save before I print. Will try to work that out tomorrow unless someone has a suggestion.  Until then, the update I made to the generate rule is below. 

	oDoc = ThisApplication.Documents.Open(dwgFile)

	Dim View As DrawingView

	For Each sheet As Sheet In oDoc.Sheets
		Sheet.Activate
		Try
			If Sheet.DrawingViews.Count > 0 Then
		        For Each View In Sheet.DrawingViews
					If View.Name = "FRONT" Then
						View.SetActiveModelState(model_state_fin)
					End If
		        Next
		    End If
		Catch

		End Try
	Next

 

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

I never thought to look in the drawing view. To pick up the model state name to set active you will need to loop through the assembly/part model state table and generate the list you want and pick one to set active. Much like you would do for design view reps. You can pick up the model document from the 

oView.ReferencedDocumentDescriptor.ReferencedDocument

 And your first post is looping through the table. 

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

insomnix
Advocate
Advocate

This is still not completely solved. I'm seeing some oddities in the pages, they are not updating correctly. I've attached some working docs. I'm hoping someone with more robust knowledge of Inventor and iLogic can help me figure this out.

 

One issue looks like the width and length parameters are not updating in the iLogic code when the model state changes, which is strange considering the image of the part changes to the new size. This can be demonstrated by running the generate rule, where it will iterate through the blanks but the message box will show the same size for every blank.

 

Another oddity that mostly happens on the last sheet, but can happen on other sheets or not at all; the field text populated from iProperties does not update. Custom or project properties doesn't appear to matter, I've dropped several UpdateWhenDone and DocumentUpdate commands, but they don't help.

 

A low priority oddity that has a work around, but is just irritating; I moved most of the IDW updates to the IPT, mostly due to the iLogic trigger on open didn't seam to be working well in the IDW. Using the code to find the sheet size of the IDW from the IPT results in numbers 10 times higher than similar code ran in the IDW. This can be demonstrated by running the check sheet size rule in each of the attached files. Maybe there is a reason for this, but I can't think what that reason might be. It's just inconsistent. 

 

Thanks for the help so far.

0 Likes
Message 8 of 8

insomnix
Advocate
Advocate

Found a solution to one of my issues. I don't know why I cannot just pull the user parameters using the standard method, but using the below does provide the correct values as I cycle through the Model States. 

Parameter.Param("length").Value
Parameter.Param("width").Value

 

0 Likes