External Rule to Create auto DWG of Part

External Rule to Create auto DWG of Part

luken_troy
Explorer Explorer
351 Views
3 Replies
Message 1 of 4

External Rule to Create auto DWG of Part

luken_troy
Explorer
Explorer

Hey all,

 

Hoping this is a quick question but I have not found anything on this all week. Any help would be greatly appreciated. I am looking to use an external rule in iLogic to automatically create a drawing of an open part. The catch is I want it to use a specific drawing template.

 

Current code I have runs it pretty well which is nice, however it doesn't use the template I want. See Code below and more notes to follow.

Imports System.IO
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Try 'to open the drawing 
	ThisApplication.Documents.Open _
	(ThisDoc.PathAndFileName(True).Replace("ipt", "idw"))
	Return 'exit rule
Catch	
End Try

'since nothing found create it...
oNewDrawing = ThisDoc.ChangeExtension(".idw")
oTemplateFolder = ThisApplication.FileOptions.TemplatesPath
Dim oList As New ArrayList

Dim Folder As New IO.DirectoryInfo(oTemplateFolder)
For Each File As IO.FileInfo In Folder.GetFiles("*.idw", IO.SearchOption.AllDirectories)
	Dim sName As New FileInfo(File.Name)	
	oList.Add(sName.Name)
Next

oTemplate = InputListBox("Select a template", oList, oList(0),"iLogic", "List")
oTemplate = oTemplateFolder & oTemplate

Dim oDrawingDoc As DrawingDocument 
oDrawingDoc = ThisApplication.Documents.Add _
(DocumentTypeEnum.kDrawingDocumentObject, oTemplate, True)

oDrawingDoc.Activate()
Dim oSheet As Sheet 
oSheet = oDrawingDoc.Sheets.Item(1)

Dim oPoint As Point2d
oPoint = ThisApplication.TransientGeometry.CreatePoint2d(10, 10)

Dim oView As DrawingView
oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint, 1, _
ViewOrientationTypeEnum.kFrontViewOrientation, _
DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)
oDrawingDoc.SaveAs(oNewDrawing, False)

 I am guessing it might be a simple fix but I have not written code quite like this before. Just not sure on what I need to insert to change it up so it works.

 

Original Code is from: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/create-drawing-with-existing-ilogic-...

 

Other link that might be similar: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/automatic-sheet-generation-for-idw/m...

 

Thanks!!

0 Likes
352 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @luken_troy.  I am not sure if this may be the cause of any current problems for you or not, but it is not a good idea to mix terms like "ThisApplication.ActiveDocument" and "ThisDoc", because in certain situations those two terms may be pointing to different documents.  Is this rule an internal one (saved within an Inventor document), or an external rule?  The 'ThisDoc' term can work a little differently in those two scenarios.  If used in an internal rule, it will pretty much always point to the 'local' document (the document that the rule is located within), but in an external rule, it will usually point to the document that was originally 'active' when the rule was first started.  There are other possible uses for it too, because it seems to be pretty dynamic.  Once you establish a variable to represent the document you want to work with, it is almost always best to use that variable from that point on within that code, instead of either one of those two terms, just to be sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

luken_troy
Explorer
Explorer

Hi @WCrihfield , I appreciate you responding. I see what you are saying. I have this rule currently saved as an external rule. My goal is to use it with a open part that I am currently editing, run the rule, and have it create a new drawing. So I would assume for this rule and other external rules that "ThisApplication.ActiveDocument" would be the best to use.


Thanks!

0 Likes
Message 4 of 4

FINET_Laurent
Advisor
Advisor

Hi @luken_troy,

 

Taking into acount what's been said earlier in the post, here is the code tweaked that does the job as intended :

Sub main 
	Dim doc As Inventor.PartDocument = ThisApplication.ActiveDocument
	Dim partDoc As Inventor.PartDocument = ThisApplication.ActiveDocument
	Dim docIdw As String = doc.FullFileName.Replace(".ipt", ".idw")
	
	If IO.File.Exists(docIdw) Then 'chekc if file exists and open it if yes
		ThisApplication.Documents.Open(docIdw, True)
		Exit Sub
	
	End If

	'since nothing found create it...
	templateFolder = ThisApplication.FileOptions.TemplatesPath
	Dim templatesNamesList As New List(Of String)
	Dim templatesLocationList As New List(Of String)
	
	Dim Folder As New System.IO.DirectoryInfo(templateFolder)
	
	For Each f As IO.FileInfo In Folder.GetFiles("*.idw", IO.SearchOption.AllDirectories)
		templatesNamesList.Add(f.Name)
		templatesLocationList.Add(f.FullName)
		
	Next

	Dim chosenTemplate  As String= InputListBox("Select a template", templatesNamesList, templatesNamesList(0),"iLogic", "List")
	Dim index As Integer = templatesNamesList.IndexOf(chosenTemplate)
	Dim template As String =  templatesLocationList(index)

	Dim newDoc As Inventor.DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, template, True)
	newDoc.Activate()
	
	Dim s As Sheet 
	s = newDoc.Sheets.Item(1)
	Dim p As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(10, 10)
	
	Dim v As DrawingView = s.DrawingViews.AddBaseView(partDoc, p, 1, ViewOrientationTypeEnum.kFrontViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)
	newDoc.SaveAs(docIdw, False)
	
End Sub

 Does this suits your needs? 

 

Kind 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