Cable & Harness Error on Event Triggered iLogic Code

Cable & Harness Error on Event Triggered iLogic Code

amirandaL4VAC
Advocate Advocate
1,195 Views
13 Replies
Message 1 of 14

Cable & Harness Error on Event Triggered iLogic Code

amirandaL4VAC
Advocate
Advocate

I'm getting this error when running my code as an event trigger:

amirandaL4VAC_0-1680036013870.png

My code has nothing to do with Cables and Harnesses. The event trigger is creating a new document. It happens whether I put it in the "Drawings" or "This Document" tab. The code runs without the error when it's not an event trigger. Other codes ran with the same trigger in the same location do not produce the error. It doesn't matter what document I'm trying to draw, I've tried it on assemblies, weldments, and parts. It still allows the whole code to run fine. I'm running Inventor 2022.

 

Here is the code:

'Drawing Setup will take the form options and create the desired drawing template
Dim oDoc As DrawingDocument 
oDoc = ThisApplication.ActiveDocument

iLogicForm.Show("Drawing Setup")

'Set the drawing size
Dim FormatName As String
FormatName = "COMPANY " + Parameter("DrawingSize")

Dim oFormat As SheetFormat
Try
	oFormat = oDoc.SheetFormats.Item(FormatName)
Catch
	MessageBox.Show("Sheet format does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End Try

'Get default sheet
Dim dSheet As Sheet
dSheet = oDoc.ActiveSheet

'Select all views
Dim oSelect As SelectSet
oSelect = oDoc.SelectSet
oSelect.Clear

Dim oView As DrawingView
For Each oView In dSheet.DrawingViews
	oSelect.Select(oView)
Next

Dim oSelectBool As Boolean
oSelectBool = oSelect.Count > 0

'Copy all views
Dim oCopy As ControlDefinition
If oSelectBool
	oCopy = ThisApplication.CommandManager.ControlDefinitions.Item("AppCopyCmd")
	oCopy.Execute
End If

'Add new sheet with chosen format
Dim oSheet As Sheet
oSheet = oDoc.Sheets.AddUsingSheetFormat(oFormat)

If oSelectBool
	'Activate new sheet and select
	oSheet.Activate
	oDoc.SelectSet.Select(oSheet)
	
	'Paste views
	Dim oPaste As ControlDefinition
	oPaste = ThisApplication.CommandManager.ControlDefinitions.Item("AppPasteCmd")
	oPaste.Execute
End If

'Delete the default sheet
dSheet.Delete

'Construct the Title Block Name and Standard Style Name from the radio button options
Dim TitleBlockName As String = "COMPANY"
Dim styleChoice As String = "Company"

If Parameter("DrawingUnits") = "Metric"
	TitleBlockName = TitleBlockName + " " + Parameter("DrawingUnits")
	styleChoice = styleChoice + " " + Parameter("DrawingUnits")
End If

If Not Parameter("DrawingType") = "Part"
	TitleBlockName = TitleBlockName + " " + Parameter("DrawingType")
End If

If Parameter("DualUnits")
	styleChoice = styleChoice + " Assembly"
Else If Not Parameter("DrawingType") = "Part"
	styleChoice = styleChoice + " " + Parameter("DrawingType")
End If

TitleBlockName = UCase(TitleBlockName + " " + Parameter("DrawingSize"))

'Check if title block name exists
Dim oTitleBlock As TitleBlockDefinition
Try
	oTitleBlock = oDoc.TitleBlockDefinitions.Item(TitleBlockName)
Catch
	MessageBox.Show("Title block name does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End Try

'Delete out the title block and add the chosen one in
oSheet.TitleBlock.Delete
oSheet.AddTitleBlock(oTitleBlock)

'Get styles library
Dim oStylesLib As DrawingStandardStylesEnumerator = oDoc.StylesManager.StandardStyles

'Change drawing style
Dim oStyle As DrawingStandardStyle
Try
	For Each oStyle In oStylesLib
		If oStyle.Name = styleChoice Then
			oDoc.StylesManager.ActiveStandardStyle = oStyle
			Exit Try
		End If
	Next
	Logger.Debug("Style " + styleChoice + " not found.")
Catch
	MessageBox.Show("Style " + styleChoice + " not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End Try

 

Accepted solutions (1)
1,196 Views
13 Replies
Replies (13)
Message 2 of 14

WCrihfield
Mentor
Mentor

Hi @amirandaL4VAC.  I'm just going to make a wild guess here, because the code looks fairly normal to me.  One of the most common issues I see is with wrong document reference that the start of a rule, or there being multiple documents being referenced within the rule, without the user knowing that it is going on.  Using the term 'ThisApplication.ActiveDocument' can be tricky at times, for different reasons.  I usually recommend that folks switch to using 'ThisDoc.Document' in that position within their iLogic rules, especially if those rules will be used in the Event Triggers.  If the code is being ran from the Event Triggers, then another document may technically be the 'active' one when it gets triggered, that therefore the code would be trying to work on that other document.  I don't know if this is the problem here or not, just a guess.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 14

amirandaL4VAC
Advocate
Advocate
Hi @WCrihfield, thanks for your comment. It's been changed to "ThisDoc.Document" since and this did not fix the issue
0 Likes
Message 4 of 14

WCrihfield
Mentor
Mentor

Copy & Paste by executing commands can act funny sometimes,  especially when the source object & target location may not actually be visible on the screen at the time the commands are being executed.  Have you tried using the normal API method for copying the view to the other sheet?  (DrawingView.CopyTo(oOtherSheet))

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 14

amirandaL4VAC
Advocate
Advocate
Accepted solution

I've narrowed down the issue to Line 61:

 

'Delete the default sheet
dSheet.Delete

 

 Not sure why that has anything to do with Cable & Harness, but I suppressed that line only and the error was gone. Sounds like a bug.

 

I got rid of the line and changed my code so I don't have to make a new sheet and delete the original anymore. Thank you for your help!

0 Likes
Message 6 of 14

DeepshipEngineering
Enthusiast
Enthusiast

I am having the same issue...

 

Starting a new drawing fires up a form asking what sheet format is required for the drawing, (then populates the sheet with sheet format rule) all of this works fine for new sheets in an existing drawing.  The harness error pops up when starting a new drawing firing this rule which is the same form and rules but then deletes the first sheet

 

' call up list of sheets from parameter "Sheet_Format"

iLogicForm.Show("Create Drawing", FormMode.Modal)


iLogicVb.RunRule("Sheet Formats")

Dim oSheet As Sheet
If MyBooleanParam = True Then
oSheet = ThisApplication.ActiveDocument.Sheets.Item(2) 
oSheet.Delete
Else
	Try 
	oSheet = ThisApplication.ActiveDocument.Sheets.Item(1) 
	oSheet.Delete
	Catch
	MessageBox.Show("2nd sheet not found?", "iLogic")
	Return
	End Try
End If

 

0 Likes
Message 7 of 14

WCrihfield
Mentor
Mentor

Hi @DeepshipEngineering.

  • Are both the iLogic Form, and the iLogic rule that are being called to show / run located within your drawing template file?
  • Are you using the event in the Event Triggers dialog named "New Document" only ; or are you also using additional events in the Event Triggers ; or are you using custom event handler codes, such as within an Inventor add-in?  Please be as specific and informational as possible.
  • Do you ever create a visible new drawing from that template by code?
    • If so, then does that trigger this event, showing the Form and running the rule?
  • Does the SheetFormat being used to create a new Sheet contain any model views?
  • Does the SheetFormat being used to create a new Sheet contain any 'linked' properties which point to 'the model'?

My first recommendation to you would be the same as earlier in this topic...edit the code you just posted, to replace "ThisApplication.ActiveDocument" code phrases with the "ThisDoc.Document" code phrase.  When the 'ThisDoc.Document' phrase is used within an 'internal' iLogic rule (saved within an Inventor document, not external), it will point to the Document that the rule is saved within, by default.  However, it is a dynamic term, supported by the iLogic add-in, so it will point to the most appropriate Document in different situations.  Such as in this case, if an iLogic rule is triggered to run because an event in the Event Triggers dialog, this phrase will represent the document that the event was triggered in/by.  If an iLogic rule is called to run with a line of code like 'iLogicVb.Automation.RunExternalRule(oDoc, "MyRule1")', then this term will represent that 'oDoc' Document that was intended for the rule to target (focus on).  I could go on about its additional functionality, but you get the point.

 

I noticed that you are showing the Form as Modal, which I believe will actually cause the iLogic rule that called it to show, to be 'paused' while it is showing, then resume once it has been closed.  That is good.  Then you seem to be running an internal iLogic rule from within the same document as the current rule.  I do not know what code is in that other rule, but it sounds like it is adding a new Sheet to the current drawing, using a SheetFormat as its source.  I believe that when we call another rule to run from within an iLogic rule, the current rule will relinquish control to that other rule until it gets done, then control comes back to the current rule, where it continues with the rest of its code.  Since a Sheet was just added a split second ago, then you are attempting to delete a sheet immediately after that, I wander if a document update between those two actions may be needed.  Something to add a slight delay for processing between the new Sheet being added, and a Sheet being deleted.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 14

DeepshipEngineering
Enthusiast
Enthusiast

Thanks for the informative reply...  I replaced "thisApplication.ActiveDocument" which worked for a day or so and then the error came back, probably something by mistake...

 

I am hoping to get permission to share the data set with you later today but am pretty sure I can not share it publicly, would that be ok and how would I do that?  I understand its not your job and you are under no obligation to help...

 

Basically, what I am trying to do is create a new drawing with a pull down menu of the type of drawing I intend to be working on.  The reason I have gone down the sheet format insert route is because the drawing types are made up of a stack of sketched symbols and some of them have prompted entries that I do not want to pop up everytime a new drawing or sheet is created.

 

The first rule that fires is an external rule event triggered by drawings, new document.  I believe this has to be external because the internal rules do not exist until the drawing is created.  This fires up an internal form to select a sheet format and then an internal rule to place that sheet format, then deletes the first sheet.  The same rule without the delete sheet is also used to add new sheets to that drawing, again to produce a sheet without all of the prompted entries in the sketched symbols popping up.

 

I tried another approach previously of using code to build these sheets, placing the symbols in specific places and editing the prompted entries on the sketched symbols placing a "SPACE" automatically but this seemed very clunky and sheet formats looked much better in terms of modifying in the future

0 Likes
Message 9 of 14

DeepshipEngineering
Enthusiast
Enthusiast
  • Are both the iLogic Form, and the iLogic rule that are being called to show / run located within your drawing template file?

An external rule fires an internal form and rule

 

  • Are you using the event in the Event Triggers dialog named "New Document" only ; or are you also using additional events in the Event Triggers ; or are you using custom event handler codes, such as within an Inventor add-in?  Please be as specific and informational as possible.

Just event trigger to fire external rule on new drawing

  • Do you ever create a visible new drawing from that template by code?
  • no 
    • If so, then does that trigger this event, showing the Form and running the rule?
  • Does the SheetFormat being used to create a new Sheet contain any model views?
  • no
  • Does the SheetFormat being used to create a new Sheet contain any 'linked' properties which point to 'the model'?
  • no
0 Likes
Message 10 of 14

WCrihfield
Mentor
Mentor

Hi @DeepshipEngineering.  Here is an updated version of the code you posted above (in Message 6) that may offer more feedback, clarity, and error prevention.  It may seem (or actually be) like overkill, but once you figure out and fix the issues you are having, using the informational feedback from this, you can simplify it back down again however you want.

 

'make sure the iLogic Log window (or at least its tab) is visible
ThisApplication.UserInterfaceManager.DockableWindows.Item("ilogic.logwindow").Visible = True
'get a reference to the drawing document, if one is available
Dim oDDoc As Inventor.DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
'if the document object obtained could not be 'Cast' (converted) to DrawingDocument then exit rule
If oDDoc Is Nothing Then
	Logger.Debug("Rule named '" & iLogicVb.RuleName & "' did not obtain a DrawingDocument!")
	Return 'or Exit Sub
End If
'make sure specific internal iLogic Form exists
Dim sFormName As String = "Create Drawing"
If Not iLogicForm.FormNames.Contains(sFormName) Then
	Logger.Debug("Could not find internal iLogic Form named '" & sFormName & "'!")
	Return 'or Exit Sub
End If
'show that specific internal iLogic Form, and capture how it gets closed
' call up list of sheets from parameter "Sheet_Format"
Dim oFRV As FormReturnValue = iLogicForm.Show(sFormName, FormMode.Modal)
'if it was canceled by Cancel button, 'X' button, or similar, then exit this rule also
If oFRV.Result = FormResult.Cancel OrElse
	oFRV.Result = FormResult.None OrElse
	oFRV.Result = FormResult.Close Then
	Return 'or Exit Sub
End If
'make sure internal iLogic rule exists in this document, then try to run it
Dim sRuleName As String = "Sheet Formats"
If iLogicVb.Automation.GetRule(oDDoc, sRuleName) IsNot Nothing Then
	Try
		Dim i As Integer = iLogicVb.Automation.RunRule(oDDoc, sRuleName)
		If i <> 0 Then 'will be -1 if it threw error, and zero if there was no error
			Logger.Debug("Internal rule named '" & sRuleName & "' encountered an error when ran!")
			'could exit this rule, since that rule encountered an error
			'Return 'or Exit Sub
		End If
	Catch
		Logger.Error("Internal rule named '" & sRuleName & "' could not be found in specified document!")
		'exit this rule, since that rule did not run
		Return 'or Exit Sub
	End Try
End If

Dim oSheets As Inventor.Sheets = oDDoc.Sheets
Dim oSheet As Inventor.Sheet
If MyBooleanParam = True Then
	If oSheets.Count > 1 Then
		oSheet = oSheets.Item(2)
		Try
			oSheet.Delete
			Logger.Info("Second Sheet Deleted")
		Catch
			Logger.Debug("Failed to delete second Sheet!")
		End Try
	End If
Else 'MyBooleanParam = False
	Try
		oSheet = oSheets.Item(1)
		oSheet.Delete
		Logger.Info("First Sheet Deleted")
	Catch
		Logger.Debug("Failed to delete first Sheet!")
		MessageBox.Show("2nd sheet not found?", "iLogic")
		Return
	End Try
End If

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 14

DeepshipEngineering
Enthusiast
Enthusiast

Hi, this is the log, still the same error

 

steveA8NCX_0-1726985551081.png

 

0 Likes
Message 12 of 14

DeepshipEngineering
Enthusiast
Enthusiast

I have tried a number of different ways and it seems that there is a bug in that you can not event trigger a new drawing that contains any delete sheet code in or it fires up the cable and harness error...  Can anyone see a way to do this?

0 Likes
Message 13 of 14

DeepshipEngineering
Enthusiast
Enthusiast

I can run this rule manually after new drawing but if I put it into the trigger or reference it with any rule triggered by new drawing I get the cable and harness error

 

Dim oSheet As Sheet
If MyBooleanParam = True Then
oSheet = ThisDoc.Document.Sheets.Item(2) 
oSheet.Delete
Else
	Try 
	oSheet = ThisDoc.Document.Sheets.Item(1) 
	oSheet.Delete
	Catch
	MessageBox.Show("2nd sheet not found?", "iLogic")
	Return
	End Try
End If

 

0 Likes
Message 14 of 14

DeepshipEngineering
Enthusiast
Enthusiast

Thanks for your help so far...  I am starting a new thread with a simplified generic data set that produces this error, hopefully get it sorted but seems like a bug

0 Likes