Monday 17 November 2014

Randomizing Family Instances in Revit

The OctaneRender for Revit plugin will apply all instances of a Family Instance with the currently select proxy.  However if you want to randomize the rotation of the Instances, how can this be done?  One method is to use a small python script.  This is done as follows:

1) Download and install Python (for your Revit version) and Vasari from https://code.google.com/p/revitpythonshell/

2) Start Revit, load the scene with the Family Instances you want to randomize

3) Select an instance of the Family Instance you want to randomize 

3) In the AddIn tab, click the new Interactive Python Shell button


4) Copy and paste the following code into the lower window

 import clr  
 import System  
   
 clr.AddReference('RevitAPI')  
 clr.AddReference('RevitAPIUI')  
 from Autodesk.Revit.DB import *  
   
 app = __revit__.Application  
 doc = __revit__.ActiveUIDocument.Document  
   
 transaction = Transaction(doc, 'Randomize family instance rotation')  
 transaction.Start()  
   
 seed = 215  
 rand = System.Random(seed)  
   
 # Get the current selection  
 selection = __revit__.ActiveUIDocument.Selection.Elements  
 if selection.Size != 1:  
   print "A single family instance must be selected"  
 else:  
   for item in selection:  
     currentElement = item   
   print "Current Selection = " + currentElement.Name  
    
   collector = FilteredElementCollector(doc)   
   collector.OfClass(FamilyInstance)  
   
   famtypeitr = collector.GetElementIdIterator()  
   famtypeitr.Reset()  
     
   count = 0  
       
   for item in famtypeitr:  
     famtypeID = item  
     faminst = doc.GetElement(famtypeID)  
     if faminst.Name == currentElement.Name:  
       # Setup the rotation axis  
       p1 = faminst.Location.Point;  
       p2 = XYZ(p1.X, p1.Y, p1.Z + 10);  
       rotationAxis = app.Create.NewLine(p1, p2, True)  
        
       # Apply a random rotation  
       faminst.Location.Rotate(rotationAxis, rand.Next(0, 360) * 180 / 3.14159);  
       count += 1  
        
   print "Randomised " + str(count) + " family instances of " + currentElement.Name  
    
 transaction.Commit()  


5) Click the Run button in the lower window toolbar


All the instances should now be randomly rotated between 0 and 360 degrees.  You can modify the above code to limit the random rotate, etc.


No comments:

Post a Comment