Inventor General Discussion

Inventor General Discussion

Valued Contributor
Posts: 70
Registered: ‎03-26-2008
Message 1 of 4 (1,285 Views)

ilogic to create view rep

1285 Views, 3 Replies
05-21-2012 10:03 PM



Is there Any code out there that will allow user to select part file then isolate and create view rep of that part with name of view rep the same as part.


The reason is we have lots of reps that isolate various components and this is very time consuming, I have had a look if there is Any customization because this would be a good user tool,also could work very well on assembly.


Any thoughts would be appreciated



Valued Contributor
Posts: 70
Registered: ‎03-26-2008
Message 2 of 4 (1,238 Views)

Re: ilogic to create view rep

07-04-2012 01:16 PM in reply to: dclunie



I have noticed curtis waguespack has posted latest blog to get all file names and folder from parts using vb code vis selectset command.Code for this  is below.


I am convinced with very minor tweaks and removing message boxes this code could be used to create my view rep  via ilogic.


  1. select part file
  2. get file name without extension (.ipt)
  3. isolte part and creat view rep with name same as step (2)
  4. lock view rep
  5. return to master view

I have had a go at compiling code to do this but i am a just a beginner at vb,any help would be appreciated?


below is link to code that curtis posted for me to creat view rep by material type.





'get currently selected component
Dim oOccurrence as ComponentOccurrence
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
  MessageBox.Show("Please select a component before running this rule.", "iLogic")
End Try

Dim doc As Document
Dim CurFileName As String

'set the selected item
oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)

'get the selected item document occurrence name
doc = oOccurrence.Definition.Document

'get the path and file name of the selected item
CurFileName = doc.FullFileName

'defines backslash as the subdirectory separator
Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar

'find the postion of the last backslash in the path
FNamePos = InStrRev(CurFileName, "\", -1)    
'get the file name with the file extension
Name = Right(CurFileName, Len(CurFileName) - FNamePos)
'get the file name (without extension)
ShortName = Left(Name, Len(Name) - 4)
'get the path of the folder containing the file
Folder_Location = Left(CurFileName, Len(CurFileName) - Len(Name))

MessageBox.Show("File Name: " & Name _
& vblf & "File Name without extension: " & ShortName _
& vblf & "File Path: " & Folder_Location _
& vblf & "Path and File Name: " & CurFileName, "iLogic")
'------------ end of ilogic--------------------


*Expert Elite*
Posts: 3,000
Registered: ‎03-08-2006
Message 3 of 4 (1,211 Views)

Re: ilogic to create view rep

07-09-2012 09:14 AM in reply to: dclunie


Hi dclunie,


I think the rule in this text file will do what you're after. It creates a vew rep for each unique part in the assembly.


Note: This rule has a flaw (if you have two instances of a part in the assembly (PartA:1 and PartA:2) and you delete instance one (PartA:1), the rule does not make a view rep. It needs to be fixed so that it looks for the first instance of each part, rather than instance " :1" of each part, but havn't had time to get that sorted.


I hope this helps.
Best of luck to you in all of your Inventor pursuits,



Here's the code for the rule, but beware of line wraps that might cause it to cut and paste incorrectly.  Use the text file instead.


'create a design view representation for each unique part in the assembly 

'define current document
Dim openDoc As Document
openDoc = ThisDoc.Document

'set a reference to the assembly component definintion.
'this assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection
Dim oCompOcc As Inventor.ComponentOccurrence 

'define view rep 
Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep names
Dim NameList As New ArrayList()

'Look at the view reps in the assembly
For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'set the list of names to the array list

'check for a Default view rep and create it if not found
If Not NameList.Contains("Default") Then
	'create Default view rep 
	oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default") 
End If

'zoom all

'look at all of the unique parts in the assembly
For Each docFile In openDoc.AllReferencedDocuments
	If docFile.DocumentType = 12290 Then '12290 is the part document enumurator
	'locate the last backslash position in the full file name
	Dim FNamePos As Long
	FNamePos = InStrRev(docFile.FullFileName, "\", -1) 
	'remove path from part file name 	
	Dim docFName As String
	docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)         
	'remove extension from part file name 
	ShortName = Left(docFName,  Len(docFName) - 4)   
		'check to see if the arraylist contains the desired view rep
		If Not NameList.Contains(ShortName) Then
		'create new View Rep 
		oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
		oViewRep.Locked = False	
		Else If NameList.Contains(ShortName) Then
		'reference existing View Rep 
		oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName) 
		oViewRep.Locked = False
		End If
		'look at all of the occurences
		For Each oCompOcc in oCompDef.Occurrences
		'locate the colon position in the occurence name
		oCompOccPos = InStrRev(oCompOcc.Name, ":") 
		'set occurence name to everything left of the colon
		oOccName = Left(oCompOcc.Name, oCompOccPos -1) 
			'set visible if name matches first occurence
	      	If oCompOcc.Name = ShortName & ":1"  Then
			oCompOcc.Visible = True
			oCompOcc.Visible = False
			End If
	End If
	'lock view rep
	oViewRep.Locked = True	

'set Default View Rep active


  solution.png  Did you find this reply helpful ? If so please use the Accept as Solution or  Kudos button below.

Valued Contributor
Posts: 70
Registered: ‎03-26-2008
Message 4 of 4 (1,102 Views)

Re: ilogic to create view rep

10-09-2012 01:19 PM in reply to: Curtis_Waguespack



I have been playing with code for a while and is very good at what it does but i was wondering can we take it to next level.


I still need view reps of parts same as before with rep name same as part without  extension,however can we get it to only create a veiwrep if it finds text in a custom property called desc2


We controll all our manufactured parts that need some kind of machining opertation to it with internal code ie bevel/ , rout/,rip to pattern/ etc .

So we have create viewreps on dwgs for these parts to show operatives how to machine parts.normally this would not be a large task but some of our models have upto 20 isolated veiwreps per assembly and we have a lot of assemblys so it is very time consuming.


Any thoughts would be appreciated










Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.