Ask to select file to place view from while opening the new drawing template.

Ask to select file to place view from while opening the new drawing template.

ABHUT_9090
Enthusiast Enthusiast
490 Views
6 Replies
Message 1 of 7

Ask to select file to place view from while opening the new drawing template.

ABHUT_9090
Enthusiast
Enthusiast

Hello,

 

I want to create simple .idw template using ilogic rule to ask to select file to place view from while opening the new drawing template. I need code for this.

 

Thanks,

 

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

WCrihfield
Mentor
Mentor

Hi @ABHUT_9090.  There are likely multiple ways to do what you want, but I have something that may work for you.  This iLogic rule, when ran, should first check to make sure a DrawingDocument is 'active', then show a Open dialog, which will allow you to browse to a file and select it.  Then, it will check to make sure something was returned from that dialog.  Then it will copy the full file name of that chosen file to something similar to Inventor's clipboard.  Then it will execute a command for creating a base drawing view.  It should pick up on the file name we sent into Inventor's clipboard as which file/model to create the view for, so you won't have to re-specify something.

 

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If

	oFile = GetFile
	If String.IsNullOrEmpty(oFile) Then Exit Sub 'or Return
	
	'The following line sends the file name to Inventor's Clipboard, this can be used to automatically
	'fill in a dialog that would normally pop-up asking for what model you want to place into your drawing.
	ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFile)
	ThisApplication.CommandManager.ControlDefinitions.Item("DrawingBaseViewCmd").Execute
End Sub

Function GetFile() As String
	Dim oFileName As String = ""
	Dim oDoc As Inventor.Document
	Dim oFileDialog As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oFileDialog)
	oFileDialog.DialogTitle = "Select A File To Create A Drawing View For."
	oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDialog.Filter = "Autodesk Inventor Files (*.iam;*.ipt:*.ipn;*.ide) | *.iam;*ipt;*.ipn;*.ide | All files (*.*)|*.*"
	oFileDialog.MultiSelectEnabled = False
	oFileDialog.OptionsEnabled = False
	oFileDialog.CancelError = True
	oFileDialog.InsertMode = False
	On Error Resume Next
	oFileDialog.ShowOpen
	If Err.Number <> 0 Then
		'something went wrong, return empty String, and exit Function
		Return oFileName
	Else	
		oFileName = oFileDialog.FileName
		Return oFileName
	End If
End Function

 

 

However, that is not everything needed.  With your target Template drawing open, you will need to place the iLogic rule that you put this code into, within the Event Triggers dialog (make sure it is on the 'This Document' tab), under the After Open Document event.  Then save the template file.  Now, every time you 'open' (not click New) it will run that rule.  However, if you want it to run when you are using the New command, then choosing this template, you will want this rule under the 'New Document' event instead.

WCrihfield_0-1638460777645.png

 

 

Another simpler approach, that wouldn't require any code, would be to start from the model file you want to create the drawing of, then use this technique:

Right click on the top browser node (on the Model browser of the open model document), then choose 'Create Drawing View' from that right click menu.  It will still want you to select a template, if you have more than one, but it will already know what model to place a view of.

WCrihfield_0-1638460425038.png

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

WCrihfield
Mentor
Mentor

The alternative to executing those two commands (the command to copy the file name to the clipboard, and the command to create a base view), would be to have the rule go ahead and create the first view.  But that would require hard coding the view's specific location/placement on the active sheet, and specifying the scale of the view, and specifying the camera/view cube orientation of the model in the view, and specifying view style (hidden lines, shaded, etc).  Here is an example of that alternative, that fills in some random values for those needed inputs and creates a view at the center of the sheet.

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	oSheet = oDDoc.ActiveSheet
	oViews = oSheet.DrawingViews
	
	oFile = GetFile
	If String.IsNullOrEmpty(oFile) Then Exit Sub 'or Return

	Dim oModel As Document = ThisApplication.Documents.Open(oFile, False)
	oPosition = ThisApplication.TransientGeometry.CreatePoint2d((oSheet.Width / 2), (oSheet.Height / 2))
	oOrientation = ViewOrientationTypeEnum.kFrontViewOrientation
	oStyle = DrawingViewStyleEnum.kHiddenLineDrawingViewStyle
	oView = oViews.AddBaseView(oModel, oPosition, .5, oOrientation, oStyle) 
End Sub

Function GetFile() As String
	Dim oFileName As String = ""
	Dim oDoc As Inventor.Document
	Dim oFileDialog As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oFileDialog)
	oFileDialog.DialogTitle = "Select A File To Create A Drawing View For."
	oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDialog.Filter = "Autodesk Inventor Files (*.iam;*.ipt:*.ipn;*.ide) | *.iam;*.ipt;*.ipn;*.ide | All files (*.*)|*.*"
	oFileDialog.MultiSelectEnabled = False
	oFileDialog.OptionsEnabled = False
	oFileDialog.CancelError = True
	oFileDialog.InsertMode = False
	On Error Resume Next
	oFileDialog.ShowOpen
	If Err.Number <> 0 Then
		'something went wrong, return empty String, and exit Function
		Return oFileName
	Else	
		oFileName = oFileDialog.FileName
		Return oFileName
	End If
End Function

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 7

ABHUT_9090
Enthusiast
Enthusiast

Hi, @WCrihfield , Thanks for sharing this code. I tried first code but it will give me view already placed in the drawing of scale 1:1 so it's not good for me. Rest all are good. when i open the new drawing template file, it is asking me to choose file after choosing file I want to stay at drawing view dialog so I can change the scale and drag the view somewhere etc. Is it possible and can you share the code for that one please.

 

Thanks,

0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor

It did that to me in my tests too.  I don't know why it goes ahead and places the view, without showing the dialog.  The command I'm using is the same command that is fired when you click on the 'Base View' tool on the 'Place Views' tab of a drawing, which normally always shows the dialog, then allows you to make changes before finalizing and placing a view.

 

Since that process isn't going to work for you, the only other process I can think of, that I have tried before, is to basically simulate the process I showed in the image above, where you right click on the top model browser node of a model document, then choose 'Create Drawing View' from the right click menu.  But that process always wants to start with a 'new' drawing document (from a chosen drawing template).  It is also not ideal,  because for me, since I have multiple drawing templates, it displays the dialog for me to choose a template to start the drawing from.  But after that it is ready to place a view of the model, with the view dialog still open and awaiting further instructions.    I believe though, that the model document must be 'visibly' opened to make it work, so I changed the line of code which opens the model, to open it visibly now, instead of invisibly.  Either way, the model must be opened in order to make drawing views of it though.

 

Here is that version of the code:

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	oSheet = oDDoc.ActiveSheet
	oViews = oSheet.DrawingViews
	
	oFile = GetFile
	If String.IsNullOrEmpty(oFile) Then Exit Sub 'or Return
	Dim oModel As Document = ThisApplication.Documents.Open(oFile, True)

	'create new drawing view of model, on in new drawing document (might ask to choose a template)
	'similar to right-clicking on top node of model, then choosing 'Create Drawing View'
	'select top Model browser node of the Model document, then execute command
	oModel.BrowserPanes.Item("Model").TopNode.DoSelect
	ThisApplication.CommandManager.ControlDefinitions.Item("UCxCreateDrawingViewCmd").Execute
	
End Sub

Function GetFile() As String
	Dim oFileName As String = ""
	Dim oDoc As Inventor.Document
	Dim oFileDialog As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oFileDialog)
	oFileDialog.DialogTitle = "Select A File To Create A Drawing View For."
	oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDialog.Filter = "Autodesk Inventor Files (*.iam;*.ipt:*.ipn;*.ide) | *.iam;*.ipt;*.ipn;*.ide | All files (*.*)|*.*"
	oFileDialog.MultiSelectEnabled = False
	oFileDialog.OptionsEnabled = False
	oFileDialog.CancelError = True
	oFileDialog.InsertMode = False
	On Error Resume Next
	oFileDialog.ShowOpen
	If Err.Number <> 0 Then
		'something went wrong, return empty String, and exit Function
		Return oFileName
	Else	
		oFileName = oFileDialog.FileName
		Return oFileName
	End If
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7

ABHUT_9090
Enthusiast
Enthusiast

Hii @WCrihfield , It's better but i can't save it as a template because first i open the new drawing first then run the rule so it will ask to open again the new drawing template and the drawing view will be in that new drawing file not my existing drawing that i opened first. if it is possible that you can explain me how to write the code for just to choose file for the view and then after the same action happens as we click on base and we have drawing view dialog with our view in the center of the drawing. 

 

Thanks,

 

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

The code I originally posted in 'Message 2' was supposed to do that, but it does not appear to be working as planned.  I'm not sure why it is not leaving the view dialog open and waiting for further instructions, instead of going ahead and placing the view with no dialog.  Seems to me like it used to work that way for me in the past, but is not working that way now.  I'm using Inventor 2022.1.1 right now.  That last command that is being executed should be simply simulating clicking that 'Base View' button.  There is an alternative method to Execute, which is called Execute2, but I don't think they would make any difference, because I believe it just deals with process timing.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)