Place a family instance by Python

Place a family instance by Python

Anonymous
Not applicable
4,809 Views
5 Replies
Message 1 of 6

Place a family instance by Python

Anonymous
Not applicable

Dear All,

 

I am trying to place a family instance by the code from: http://wiki.theprovingground.org/revit-api-py-family. I opened an Mass.rtf file, pasted the code in Interactive Python Shell, but it doesn’t work.

 

import clr
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
t = Transaction(doc, 'Create family instance.')
 
t.Start()
 
#Family symbol name to place.
symbName = 'BoxFamily'
 
#create a filtered element collector set to Category OST_Mass and Class FamilySymbol 
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_Mass)
collector.OfClass(FamilySymbol)
 
famtypeitr = collector.GetElementIdIterator()
famtypeitr.Reset()
 
#Search Family Symbols in document.
for item in famtypeitr:
    famtypeID = item
    famsymb = doc.get_Element(famtypeID)
 
    #If the FamilySymbol is the name we are looking for, create a new instance.
    if famsymb.Family.Name == symbName:
 
        #location to place family
        loc = XYZ(0,0,0) 
 
        #NewFamilyInstance()
        familyInst = doc.FamilyCreate.NewFamilyInstance(loc, famsymb, Structure.StructuralType.NonStructural)
 
t.Commit()
 
__window__.Close()

 

As a poor rookie, I was confused by the 'BoxFamily' which I guess it is a family instance, introduced and defined by the programmer. So I want to print all family names, so I tried: 

 

for item in famtypeitr:

    famtypeID = item

    famsymb = doc.get_Element(famtypeID)

    print (famsymb.Family.Name)

 

Still nothing. 

 

So can anyone tell me how to print all the family name? and how to define a symbol such as the 'BoxFamily' so I can place my own instances?

 

Thank you for your help to a rookie who has now been crazy.

 

Robin

0 Likes
4,810 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk

Welcome to the Revit API!

 

Do you understand the concepts of families, types and instances?

 

Do you have any experience with Revit?

 

I think you should maybe start with something simpler and gather some experience first.

 

Here are some hints on getting started with the Revit API:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 

Good luck and have fun!

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6

Anonymous
Not applicable

Dear Jeremy, thanks for your kindly reply. I have swum in your Building Coder and BBS in the last two months, luckily I got survived.

 

I need to create a new family and insert into the project first, then I can use the code to place the family instance.  Thanks.

0 Likes
Message 4 of 6

Anonymous
Not applicable


@Anonymous wrote:

Dear Jeremy, thanks for your kindly reply. I have swum in your Building Coder and BBS in the last two months, luckily I got survived.

 

I need to create a new family and insert into the project first, then I can use the code to place the family instance.  Thanks.


@Anonymous wrote:

Dear Jeremy, thanks for your kindly reply. I have swum in your Building Coder and BBS in the last two months, luckily I got survived.

 

I need to create a new family and insert into the project first, then I can use the code to place the family instance.  Thanks.



Hi Chunhuang, I also need to place a family with python and I came across the same code from the the website. However, I have the same issue as you had before. May I know how did you solve it? I inserted the family first and then ran the code, bus still got nothing. I am assuming the I input the wrong family symbol? Can you give me an example of what the family symbol is? I thought it is just the family type?  Thanks! 

0 Likes
Message 5 of 6

stever66
Advisor
Advisor

I tried to copy and paste your code, but with python I constantly get "unexpected indent" errors even when everything is typed perfectly.  Even if I delete and reinsert all the returns and tabs, still the same thing.  Its the one and only reason I quit trying to do anything with python.

 

However, make sure you have a family loaded into the project, and the family should be a Mass type (create the family using the Mass.rft template.)   I would name the family "BoxFamily", and if that doesn't work, try also creating a family type called "BoxFamily".

 

Anyhow, I do have one suggestion for you.  Try using the Revit Macro Editor that comes as part of Revit.  


Goto the "manage" ribbon, select "Macro Manager", and create a new Module making sure to pick Python.  With a new Python Module,  Revit adds a few Python code samples that you can enable and run by changing "if False" to "if True".  

0 Likes
Message 6 of 6

Anonymous
Not applicable

Dear Cheng, attached please find the family symbol 'cube' for your reference. See if it can work.

0 Likes