Activate Referenced Doc

Activate Referenced Doc

Anonymous
Not applicable
767 Views
6 Replies
Message 1 of 7

Activate Referenced Doc

Anonymous
Not applicable

Happy Friday!!

i have an issue that i thought wouldn't be an issue. I have a script that is used for on drawings of sheetmetal parts. However before it does anything to the drawing it's supposed to open the part referenced in the drawing. The problem as i see it is, i can't do anything to the part without first activating it.  My code is shown below:

 

Dim oDoc as Document = ThisDrawing.Document
Dim refDoc As Document = ThisDrawing.ModelDocument
auto = iLogicVb.Automation

refDoc.Activate()
auto.RunExternalRule(refDoc,"DoThis")
auto.RunExternalRule(refDoc, "ThenThis")

iLogicVb.RunExternalRule("FinallyThis")

 Any help would be greatly appreciated!!

 

 

0 Likes
768 Views
6 Replies
Replies (6)
Message 2 of 7

FINET_Laurent
Advisor
Advisor

Evening,

 

What is exactly the issue ?

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 7

FINET_Laurent
Advisor
Advisor

From the little I understood :

 

Dim oView As DrawingView
	oView = ThisApplication.CommandManager.Pick(kDrawingViewFilter,"Pick a view")

Dim Path As String = oView.ReferencedDocumentDescriptor.FullDocumentName

Dim oRefDoc As Document
	oRefDoc = ThisApplication.Documents.Open(Path, True)

auto = iLogicVb.Automation
	auto.RunExternalRule(oRefDoc,"DoThis")

oRefDoc.Close

 

Maybe this is a starting point..

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 4 of 7

Anonymous
Not applicable

@FINET_Laurent  the problem is that 'refDoc' is not actually fetching the referenced document. 

Dim refDoc As Document = ThisDrawing.ModelDocument

 

And so i thought i would need to activate the document first, thus refDoc.Activate().  @WCrihfield informed me that since the rule was already specifying the document, there was no need to activate the document first (makes perfect sense).

When i actually run the rule i get this error:

Error in rule: DXFit, in document: P07A00644-0__ATL_000.ipt

Public member 'ComponentDefinition' on type '_DocumentClass' not found.

 

This is the whole code:

Dim oDoc As Document = ThisDrawing.Document
Dim refDoc As Document = ThisDrawing.ModelDocument

auto = iLogicVb.Automation

Try
auto.RunExternalRule(refDoc,"BurnNumber")
auto.RunExternalRule(refDoc, "DXFit")
Catch
auto.RunExternalRule(refDoc,"BurnNumber")
End Try
auto.RunExternalRule(oDoc, "PDFit")

 

0 Likes
Message 5 of 7

Anonymous
Not applicable

And the error message said there was an error with the external rule 'DXF'it' but it works fine alone.

0 Likes
Message 6 of 7

FINET_Laurent
Advisor
Advisor

The code I posted is working flawless, I'm not sure what else you need.

Maybe I'm understanding things wrong  ?

 

Regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

OK. Here is a much longer version of the code, in which I've added a lot of error checking code to help eliminate possible errors, and inform you better about any errors that do still happen.  If you still receive error message with this code, please provide screen captures of each tab of the error messages in the next post so we can better understand them.  Also, if you see any of the messages I have inserted into the code as error checks, let us know about those too.

 

Also, if this code still doesn't fix the problem, or at least let us know better where the problem is, we may need to see the contents of those other rules you are trying to run.  Even though they may work OK when ran individually from the model file, we may need to make slight changes to them that will allow them to work better when being ran from the drawing this way.   A common problem in this type of situation, is when the other rules refer to their target document as 'ThisApplication.ActiveDocument', when the active document is almost always the document that is open/active when the original rule is ran (in this case the drawing).  Sometimes you can change this reference in the other rules to ThisDoc.Document to fix this, but not always.

 

Anyways, here's the newly expanded code for you to try:  (I've included lots of comment lines in there too.)

'Making sure the 'active' document is a Drawing document
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If

'defining the variable as a DrawingDocument, and setting its value
Dim oDDoc As DrawingDocument = ThisDrawing.Document

'Now ckecking to make sure this drawing has a Model document (to help eliminate possible errors)
If ThisDrawing.ModelDocument Is Nothing Then
	MsgBox("The model document for this drawing was not found. Exiting.", vbOKOnly + vbCritical, "MODEL NOT FOUND")
	Exit Sub
End If

'Now we're checking the 'model' document's DocumentType,
'to make sure it is not only a Part but a Sheet Metal Part.
If ThisDrawing.ModelDocument.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	MsgBox("The 'model' document is an assembly. It must be a Sheet Metal Part for this rule to work. Exiting.", vbOKOnly + vbCritical, "Wrong Model Type")
	Exit Sub
End If

'defining the variable as a PartDocument, and setting its value, now that we know for sure that is is a Part
'Also just to make absolutely sure the Model document is open and active,
'since we will be dealing with it first, before the drawing
Dim oMDoc As PartDocument = ThisApplication.Documents.Open(ThisDrawing.ModelDocument.FullDocumentName, False)
oMDoc.Activate

'now we need to make sure it is a Sheet Metal Part, not just a regular Part
'There is more than one way to check this, but I like to check within its iProperties
'the document needs to be open (at some capacity) before you can check its iProperties
If oMDoc.PropertySets.Item("Design Tracking Properties").Item("Document SubType Name") <> "Sheet Metal" Then
	MsgBox("The model is a Part, but not a Sheet Metal Part. Exiting.", vbOKOnly + vbCritical, "NOT SHEET METAL")
	Exit Sub
End If

Dim oAuto As IiLogicAutomation = iLogicVb.Automation

Try
	oAuto.RunExternalRule(oMDoc, "BurnNumber")
Catch oEx As Exception
	MsgBox("Something went wrong while trying to run 'BurnNumber'." & vbCrLf & _
	"The Error Message is as follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace' is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its 'Source' is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, " ")
End Try

Try
	oAuto.RunExternalRule(oMDoc, "DXFit")
Catch oEx As Exception
	MsgBox("Something went wrong while trying to run 'DXFit'." & vbCrLf & _
	"The Error Message is as follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace' is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its 'Source' is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, " ")
End Try

Try
	oDDoc.Activate
Catch
End Try

Try
	'oMDoc.Save
	oMDoc.Close(True) 'True means Skip Saving
Catch
	oMDoc.ReleaseReference
Finally
	'do nothing.
	'you may not even need to close it since your active drawing is still referencing it.
	'when you close the drawing, it will likely release the reference to the model file too
End Try

Try
	oAuto.RunExternalRule(oDDoc, "PDFit")
Catch oEx As Exception
	MsgBox("Something went wrong while trying to run 'PDFit'." & vbCrLf & _
	"The Error Message is as follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace' is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its 'Source' is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, " ")
End Try

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes