load and place an familyobject => System.NullReferenceException

load and place an familyobject => System.NullReferenceException

Anonymous
Not applicable
788 Views
1 Reply
Message 1 of 2

load and place an familyobject => System.NullReferenceException

Anonymous
Not applicable

Hi all, 

 

i am trying to load and place an familyobject.

i am using this topic from jeremy tammik: 

http://thebuildingcoder.typepad.com/blog/2013/06/family-api-add-in-load-family-and-place-instances.h...

 

 

my code since now:

 

 

public void FloorMacro()
		{
			
		UIDocument uiDoc = this.Application.ActiveUIDocument;
                Document doc = uiDoc.Document;

                XYZ location = new XYZ(0, 0, 0);
                string filePath = @"D:\Familie1.rfa";
                Family family;
                
 
                       using( Transaction trans = new Transaction( doc ) ) {
                	    trans.Start("start");
                
                           //load the family
                           doc.LoadFamily(filePath, out family);
                
      
                           trans.Commit();
                       }

                FamilySymbol symbol = null;
                
                      //choose the familysymbol
                      foreach( ElementId id in family.GetFamilySymbolIds() )  {
                	    symbol = doc.GetElement( id ) as FamilySymbol;
                		// Our family only contains one
                		// symbol, so pick it and leave
                		break;
              	      }
                
                symbol.Activate();
                
                
                //place the familyinstance
             	uiDoc.PromptForFamilyInstancePlacement( symbol );
		}

the problem is that i always get this error when i run the macro:

 

System.NullReferenceEcxeption

 

 

I hope that someone can help me to solve that Problem.

 

Thanks a lot!

 

 

0 Likes
Accepted solutions (1)
789 Views
1 Reply
Reply (1)
Message 2 of 2

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Here is the solution

        public void FloorMacro()
        {

            UIDocument uiDoc = this.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;

            XYZ location = new XYZ(0, 0, 0);
            string filePath = @"E:\workshop\Sample Revit files\Families\Fire Alarm Detectors.rfa";
            string familyName = "Fire Alarm Detectors";

            Family family;


            using (Transaction trans = new Transaction(doc))
            {
                trans.Start("start");

                //load the family
                doc.LoadFamily(filePath, out family);


                trans.Commit();
            }

            FamilySymbol symbol = null;

            if (family == null)
            {
                FilteredElementCollector FamiliesCollector = new FilteredElementCollector(doc);

                FamiliesCollector.OfClass(typeof(Family));

                var families = from m_family in FamiliesCollector
                               where m_family.Name.ToLower() == familyName.ToLower()
                               select m_family;
                family = families.Cast<Family>().FirstOrDefault<Family>();
            }

            //choose the familysymbol
            foreach (ElementId id in family.GetFamilySymbolIds())
            {
                symbol = doc.GetElement(id) as FamilySymbol;
                // Our family only contains one
                // symbol, so pick it and leave
                break;
            }
            using (Transaction trans = new Transaction(doc))
            {
                trans.Start("start");
                symbol.Activate();

                trans.Commit();
            }

            //place the familyinstance
            uiDoc.PromptForFamilyInstancePlacement(symbol);
        }

If this reply satisfies your need don't forget to mark this as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes