Creating a pipe or flex pipe by loading a family

Creating a pipe or flex pipe by loading a family

Anonymous
Not applicable
1,396 Views
2 Replies
Message 1 of 3

Creating a pipe or flex pipe by loading a family

Anonymous
Not applicable

I'm completely new to the Revit API and am having trouble simply loading a family and using it to create a pipe or flex pipe. I'm sure there is something simple I am missing, and I've had success with things like doors and tables, however I'm completely baffled with creating even a simple pipe that goes from A to B beyond just the default pipe option (I've included the code I used to make a default flex pipe and default pipe - my only success). I have not been successful in finding example code online (I have scoured The Building Coder and have not found enough pieces to put this together). If someone can give me a basis to work from I would be so grateful.

 

Working FlexPipe:

private static void CreateFlexPipe(Document newDoc, Wall wall)
        {

            FilteredElementCollector collector = new FilteredElementCollector(newDoc);
            collector.OfClass(typeof(FlexPipeType));
            ElementId pipeTypeId = collector.FirstElementId();

            FilteredElementCollector sysCollector = new FilteredElementCollector(newDoc);
            sysCollector.OfClass(typeof(PipingSystemType));
            ElementId pipeSysTypeId = sysCollector.FirstElementId();

            Level levelId = newDoc.GetElement(wall.LevelId) as Level;

            FlexPipe pipe = null;
            if (pipeTypeId != ElementId.InvalidElementId && pipeSysTypeId != ElementId.InvalidElementId)
            {
                List<XYZ> points = new List<XYZ>();
                points.Add(new XYZ(0, 0, 0));
                points.Add(new XYZ(10, 10, 0));
                points.Add(new XYZ(10, 0, 0));

                pipe = FlexPipe.Create(newDoc, pipeSysTypeId, pipeTypeId, levelId.Id, points);
            }
        }

Working Pipe:

private static void CreatePipe(Document newDoc, Wall wall)
        {

            FilteredElementCollector collector = new FilteredElementCollector(newDoc);
            collector.OfClass(typeof(PipeType));
            ElementId pipeTypeId = collector.FirstElementId();

            FilteredElementCollector sysCollector = new FilteredElementCollector(newDoc);
            sysCollector.OfClass(typeof(PipingSystemType));
            ElementId pipeSysTypeId = sysCollector.FirstElementId();

            Level levelId = newDoc.GetElement(wall.LevelId) as Level;

            Pipe pipe1 = null;

            if (pipeTypeId != ElementId.InvalidElementId && pipeSysTypeId != ElementId.InvalidElementId)
            {
                XYZ p1 = new XYZ(0, 0, 0);
                XYZ p2 = new XYZ(10, 10, 0);

                pipe1 = Pipe.Create(newDoc, pipeSysTypeId, pipeTypeId, levelId.Id, p1, p2);

            }

        }

Also here is where I am at with trying to load a family:

private static void TestLoadFamily(Document newDoc)
        {
            string filename = @"C:\ProgramData\Autodesk\RVT 2020\Libraries\US Metric\Pipe\Actual Pipe\JONATHAN39S_METRIC_PIPE_HORIZONTAL_30MM_PIPE_THICK_2825.rfa";

            FilteredElementCollector levelCollector = new FilteredElementCollector(newDoc);
            levelCollector.OfClass(typeof(Level));
            ElementId someLevelId = levelCollector.FirstElementId();

            FilteredElementCollector sysCollector = new FilteredElementCollector(newDoc);
            sysCollector.OfClass(typeof(PipingSystemType));
            ElementId pipeSysTypeId = sysCollector.FirstElementId();

            using (Transaction familyTrans = new Transaction(newDoc, "Use a Family"))
            {
                familyTrans.Start();

                Family family = null;
                if (!newDoc.LoadFamily(filename, out family))
                {
                    throw new Exception("Unable to load " + filename);
                }

                ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();

                int count = 0;
                foreach (ElementId id in familySymbolIds)
                {
                    if (count == 0)
                    {
                        FamilySymbol pipeSymbol = family.Document.GetElement(id) as FamilySymbol;
                        if (!pipeSymbol.IsActive)
                        {
                            pipeSymbol.Activate();
                            newDoc.Regenerate();
                        }
                        Pipe pipe = null;
                        XYZ p1 = new XYZ(0, 0, 0);
                        XYZ p2 = new XYZ(10, 10, 0);

                        pipe = Pipe.Create(newDoc, pipeSysTypeId, pipeSymbol.Id, someLevelId, p1, p2);
                    }
                    count++;
                }
                familyTrans.Commit();
            }
        }

This errors with: "The pipe type pipeTypeId is not valid pipe type."

 

*Note: I am using the DesignAutomationHandler and build within another project. 

0 Likes
Accepted solutions (1)
1,397 Views
2 Replies
Replies (2)
Message 2 of 3

jlpgy
Advocate
Advocate
Accepted solution

Hi:

Notice that <Duct>, <Pipe> as well as <FlexDuct> and <FlexPipe> are all "Built In" elements.

They are very basic types of elements in Revit DB. They have their own "types", such as <DuctType> or <PipeType>. Their *Family* are all built-in. For example, <Duct> has only 3 families: Rectangle, Round, Oval; <Pipe> has only 1 family: Pipe Types.(don't understand why Autodesk gave such a confusing name to this family.....).

 

There are also lots of elements with the type <FamilyInstance>. The class <FamilySymbol> is a "type" of a <FamilyInstance> element. <FamilyInstance> elements have their own "type" <FamilySymbol>. And <FamilySymbol> have their own family <Family>.

 

But, for Revit DB "built in" elements, such as a <Pipe> element, it is of a "type" <PipeType>.

 

There are 2 parallel world in Revit API world, one is *Built-In* elements, another is <FamilyInstance> elements.

All I can tell is that "Loading a family has nothing to do with creating a Pipe". 🙂

单身狗;代码狗;健身狗;jolinpiggy@hotmail.com
Message 3 of 3

Anonymous
Not applicable

Thank you! The language helps immensely! I think I can do better searching now that I know what I'm looking for. 

0 Likes