コード例 #1
0
        public static void Calculate(UIApplication uiApp)
        {
            Document   doc   = uiApp.ActiveUIDocument.Document;
            UIDocument uidoc = uiApp.ActiveUIDocument;

            try
            {
                using (TransactionGroup txGp = new TransactionGroup(doc))
                {
                    txGp.Start("Calculate height of hangers");

                    FilteredElementCollector lvlCol   = new FilteredElementCollector(doc);
                    List <Element>           levels   = lvlCol.OfClass(typeof(Level)).ToList();
                    List <string>            lvlNames = levels.Select(x => x.Name).ToList();

                    //The idea is to select a different lvl for each differen reflvl
                    //To start with, we do calculations for all hangers
                    //As all our projects are with two levels only

                    BaseFormTableLayoutPanel_Basic lvlSelector = new BaseFormTableLayoutPanel_Basic(lvlNames);
                    lvlSelector.ShowDialog();

                    string lvlName = lvlSelector.strTR;

                    var    topLvl          = fi.GetElements <Level, BuiltInParameter>(doc, BuiltInParameter.DATUM_TEXT, lvlName).First();
                    double topLvlElevation = topLvl.Elevation;

                    //Collect elements
                    var hangerSupports = fi.GetElements <FamilyInstance, Guid>(doc, new Guid("e0baa750-22ba-4e60-9466-803137a0cba8"), "Hænger");
                    //Hangers must be of Category PipeAccessory
                    HashSet <Element> allHangers = new HashSet <Element>(hangerSupports.Cast <Element>()
                                                                         .Where(x => x.Category.Id.IntegerValue == (int)BuiltInCategory.OST_PipeAccessory));

                    using (Transaction trans1 = new Transaction(doc))
                    {
                        trans1.Start("Calculate height");

                        foreach (Element hanger in allHangers)
                        {
                            ElementId refLvlId        = hanger.LevelId;
                            Level     refLvl          = (Level)doc.GetElement(refLvlId);
                            double    refLvlElevation = refLvl.Elevation;
                            double    lvlHeight       = topLvlElevation - refLvlElevation;
                            hanger.LookupParameter("LevelHeight").Set(lvlHeight);

                            Parameter offsetPar     = hanger.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
                            double    offsetFromLvl = offsetPar.AsDouble();
                            hanger.LookupParameter("PipeOffsetFromLevel").Set(offsetFromLvl);
                        }
                        trans1.Commit();
                    }
                    txGp.Assimilate();
                }
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
                //return Result.Failed;
            }
        }
コード例 #2
0
        public static Result StartCreating(UIApplication UiApp)
        {
            UIApplication uiApp = UiApp;
            Document      doc   = uiApp.ActiveUIDocument.Document;
            UIDocument    uidoc = uiApp.ActiveUIDocument;

            try
            {
                using (TransactionGroup txGp = new TransactionGroup(doc))
                {
                    txGp.Start("Create Instrumentation!");

                    Pipe           selectedPipe = null;
                    XYZ            iP           = null;
                    string         operation    = string.Empty;
                    string         direction    = string.Empty;
                    string         PipeTypeName;
                    PipeType       pipeType;
                    double         size;
                    FamilyInstance olet;
                    ElementId      curLvlId;
                    ElementId      curPipingSysTypeId;
                    ElementId      curPipeTypeId;

                    //Section to detect if an auto air vent selected and apply fittings to other end
                    ElementId eId             = uidoc.Selection.GetElementIds().FirstOrDefault();
                    Element   selectedElement = null;
                    Parameter namePar;
                    string    famTyp = string.Empty;
                    if (eId != null)
                    {
                        selectedElement = doc.GetElement(eId);
                        namePar         = selectedElement.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM);
                        famTyp          = namePar.AsValueString();
                        operation       = "Add fittings to ML";
                    }

                    //If selection is not an Auto Air Vent, continue as normal
                    if (famTyp != "SpiroTop_AB050-R004: Standard")
                    {
                        //TODO: Implement a selection of: 1) Point on a pipe selected directly 2) Distance from element on the pipe
                        using (Transaction trans1 = new Transaction(doc))
                        {
                            trans1.Start("SelectPipePoint");
                            (selectedPipe, iP) = SelectPipePoint(doc, uidoc);
                            trans1.Commit();
                        }

                        //Select operation to perform
                        BaseFormTableLayoutPanel_Basic op = new BaseFormTableLayoutPanel_Basic(Cursor.Position.X, Cursor.Position.Y, lad.Operations());
                        op.ShowDialog();
                        operation = op.strTR;

                        //Select the direction to create in
                        BaseFormTableLayoutPanel_Basic ds = new BaseFormTableLayoutPanel_Basic(Cursor.Position.X, Cursor.Position.Y, lad.Directions());
                        ds.ShowDialog();
                        direction = ds.strTR;
                        //ut.InfoMsg(ds.strTR);
                    }

                    switch (operation)
                    {
                    case "Auto ML (Udlufter)":
                        using (Transaction trans2 = new Transaction(doc))
                        {
                            trans2.Start("Auto ML");
                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse sockolet");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            //"DN15-SM-EL: SM-EL"
                            Element cpValve = createNextElement(doc, olet, "DN15-SM-EL: SM-EL") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            Element union1 = createNextElement(doc, cpValve,
                                                               "PIF_Cast Iron 281 hex nipple RH and LH thread ISO EN N8 R-L_GF: DN10 - DN50, Galvanised",
                                                               "connection_diameter1", 15.0) ??
                                             throw new Exception("Creation of union1 failed for some reason!");
                            doc.Regenerate();

                            Element mlValve = createNextElement(doc, union1, "SpiroTop_AB050-R004: Standard") ??
                                              throw new Exception("Creation of mlValve failed for some reason!");

                            trans2.Commit();
                        }
                        break;

                    case "Add fittings to ML":
                        using (Transaction trans21 = new Transaction(doc))
                        {
                            trans21.Start("Finish Auto ML");

                            Element union2 = createNextElement(doc, selectedElement,
                                                               "PIF_Cast Iron 330 union flat seat ISO-EN U1_GF: DN8 - DN100, Black",
                                                               "connection_diameter1", 15.0, true) ??
                                             throw new Exception("Creation of union2 failed for some reason!");

                            Element adapter = createNextElement(doc, union2,
                                                                "PIF_Mapress-StSt Adapter MT_Geberit: Var. DN",
                                                                "connection_diameter1", 15.0) ??
                                              throw new Exception("Creation of adapter failed for some reason!");

                            trans21.Commit();
                        }
                        break;

                    case "PT (Tryktransmitter)":
                        using (Transaction trans3 = new Transaction(doc))
                        {
                            trans3.Start("PT");

                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse sockolet");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            //"DN15-SM-EL: SM-EL"
                            Element cpValve = createNextElement(doc, olet, "DN15-SM-EL: SM-EL") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            Element instr = createNextElement(doc, cpValve, "Sitrans_P200: Standard") ??
                                            throw new Exception("Creation of instrument failed for some reason!");

                            trans3.Commit();
                        }
                        break;

                    case "PI (Manometer)":
                        using (Transaction trans4 = new Transaction(doc))
                        {
                            trans4.Start("Manometer");
                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse sockolet");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            Element cpValve = createNextElement(doc, olet, "DN15-SM-EL: SM-EL") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            Element instr = createNextElement(doc, cpValve, "WIKA.Manometer.233.50.100: Standard") ??
                                            throw new Exception("Creation of instrument failed for some reason!");

                            trans4.Commit();
                        }
                        break;

                    case "TT (Temp. transmitter)":
                        using (Transaction trans5 = new Transaction(doc))
                        {
                            trans5.Start("Temperaturtransmitter");
                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse, termolomme");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            Element cpValve = createNextElement(doc, olet, "WIKA.Termolomme.TW55-6: L200.U65.G1/2.9") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            //TODO: Places the instrument at wrong angle!!!!
                            Element instr = createNextElement(doc, cpValve, "Sitrans_TS500: Standard") ??
                                            throw new Exception("Creation of instrument failed for some reason!");

                            trans5.Commit();
                        }
                        break;

                    case "TI (Termometer)":
                        using (Transaction trans6 = new Transaction(doc))
                        {
                            trans6.Start("Termometer");
                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse, termolomme");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            Element cpValve = createNextElement(doc, olet, "WIKA.Termolomme.TW55-6: L200.U65.G1/2.9") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            //TODO: Places the instrument at wrong angle!!!!
                            Element instr = createNextElement(doc, cpValve, "WIKA.Termometer.A52.100: Standard") ??
                                            throw new Exception("Creation of instrument failed for some reason!");

                            trans6.Commit();
                        }
                        break;

                    case "PS (Pressostat)":
                        using (Transaction trans7 = new Transaction(doc))
                        {
                            trans7.Start("Pressostat");
                            Element dummyPipe;
                            (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, 15, "Stålrør, sømløse sockolet");
                            doc.Delete(dummyPipe.Id);
                            doc.Regenerate();

                            Element cpValve = createNextElement(doc, olet, "DN15-SM-EL: SM-EL") ??
                                              throw new Exception("Creation of cpValve failed for some reason!");

                            Element instr = createNextElement(doc, cpValve, "Danfoss_pressostat_017-519166: Standard") ??
                                            throw new Exception("Creation of instrument failed for some reason!");

                            trans7.Commit();
                        }
                        break;

                    case "Pipe":
                        #region "Case PIPE"
                        //Select type of Olet
                        BaseFormTableLayoutPanel_Basic oletSelector = new BaseFormTableLayoutPanel_Basic(Cursor.Position.X, Cursor.Position.Y, lad.PipeTypeByOlet());
                        oletSelector.ShowDialog();
                        if (oletSelector.strTR.IsNullOrEmpty())
                        {
                            return(Result.Cancelled);
                        }
                        PipeTypeName = oletSelector.strTR;
                        //ut.InfoMsg(PipeTypeName);
                        pipeType = fi.GetElements <PipeType, BuiltInParameter>(doc, BuiltInParameter.SYMBOL_NAME_PARAM, PipeTypeName).First();

                        //Limit sizes for Olets
                        List <string> sizeListing;
                        switch (oletSelector.strTR)
                        {
                        case "Stålrør, sømløse, termolomme":
                        case "Stålrør, sømløse sockolet":
                            sizeListing = lad.SockoletList();
                            break;

                        case "Stålrør, sømløse weldolet":
                            sizeListing = lad.WeldoletList();
                            break;

                        default:
                            sizeListing = lad.SizeList();
                            break;
                        }

                        if (oletSelector.strTR != "Stålrør, sømløse")
                        {
                            BaseFormTableLayoutPanel_Basic sizeSelector = new BaseFormTableLayoutPanel_Basic(Cursor.Position.X, Cursor.Position.Y, sizeListing);
                            sizeSelector.ShowDialog();
                            size = double.Parse(sizeSelector.strTR);
                        }
                        else
                        {
                            size = selectedPipe.Diameter.FtToMm().Round(0);
                        }

                        curLvlId           = selectedPipe.ReferenceLevel.Id;
                        curPipingSysTypeId = selectedPipe.MEPSystem.GetTypeId();
                        curPipeTypeId      = pipeType.Id;

                        if (oletSelector.strTR != "Stålrør, sømløse")
                        {
                            using (Transaction trans2 = new Transaction(doc))
                            {
                                trans2.Start("Create Olet");

                                Element dummyPipe;
                                (olet, dummyPipe) = CreateOlet(doc, iP, direction, selectedPipe, size, oletSelector.strTR);
                                if (olet == null || dummyPipe == null)
                                {
                                    txGp.RollBack();
                                    return(Result.Cancelled);
                                }
                                ;

                                //dbg.PlaceAdaptiveFamilyInstance(doc, "Marker Line: Red", offsetPoint, dirPoint);

                                trans2.Commit();
                            }
                        }
                        else if (oletSelector.strTR == "Stålrør, sømløse")
                        {
                            using (Transaction trans2 = new Transaction(doc))
                            {
                                trans2.Start("Create Tee");

                                Pipe dummyPipe;
                                dummyPipe = CreateTee(doc, iP, direction, selectedPipe, size, oletSelector.strTR);
                                if (dummyPipe == null)
                                {
                                    txGp.RollBack();
                                    return(Result.Cancelled);
                                }
                                ;

                                //dbg.PlaceAdaptiveFamilyInstance(doc, "Marker Line: Red", offsetPoint, dirPoint);

                                trans2.Commit();
                            }
                        }
                        #endregion
                        break;

                    default:
                        return(Result.Cancelled);
                    }

                    txGp.Assimilate();
                }

                return(Result.Succeeded);
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #3
0
        public static Result CreatePipeFromConnector(UIApplication uiApp)
        {
            Document doc = uiApp.ActiveUIDocument.Document;

            bool ctrl = false;

            if ((int)Keyboard.Modifiers == 2)
            {
                ctrl = true;
            }

            string pipeTypeName = MEPUtils.Properties.Settings.Default.PipeCreator_SelectedPipeTypeName;

            //If the name of pipeType is null or empty for some reason -- reinitialize
            if (string.IsNullOrEmpty(pipeTypeName))
            {
                ctrl = true;
            }

            if (ctrl)
            {
                FilteredElementCollector colPipeTypes = new FilteredElementCollector(doc);
                var pipeTypes = colPipeTypes.OfClass(typeof(PipeType)).ToElements();

                var pipeTypeNames = colPipeTypes.Select(x => x.Name).ToList();

                int count = pipeTypeNames.Count;

                var pc = new PipeTypeSelector(uiApp, pipeTypeNames);
                pc.ShowDialog();

                pipeTypeName = pc.pipeTypeName;
            }

            try
            {
                //One element selected, creates pipe at random connector
                //Or an elbow for pipe
                //Two elements selected: Creates pipe between them
                //NOTE: the connectors must be aligned
                Selection selection           = uiApp.ActiveUIDocument.Selection;
                ICollection <ElementId> elIds = selection.GetElementIds();
                if (elIds.Count == 2)
                {
                    Element firstEl  = doc.GetElement(elIds.First());
                    Element secondEl = doc.GetElement(elIds.Last());

                    HashSet <Connector> firstCons  = mp.GetALLConnectorsFromElements(firstEl);
                    HashSet <Connector> secondCons = mp.GetALLConnectorsFromElements(secondEl);

                    var listToCompare = new List <(Connector firstCon, Connector secondCon, double Distance)>();

                    foreach (Connector c1 in firstCons)
                    {
                        foreach (Connector c2 in secondCons)
                        {
                            listToCompare.Add((c1, c2, c1.Origin.DistanceTo(c2.Origin)));
                        }
                    }

                    var(firstCon, secondCon, Distance) = listToCompare.MinBy(x => x.Distance).FirstOrDefault();

                    using (Transaction tx = new Transaction(doc))
                    {
                        //Get the typeId of the selected or read PipeType
                        var filter = fi.ParameterValueGenericFilter(doc, pipeTypeName, BuiltInParameter.ALL_MODEL_TYPE_NAME);
                        FilteredElementCollector col = new FilteredElementCollector(doc);
                        var pipeType = col.OfClass(typeof(PipeType)).WherePasses(filter).ToElements().FirstOrDefault();
                        if (pipeType == null)
                        {
                            throw new Exception("Collection of PipeType failed!");
                        }

                        //LevelId can be null -> work around
                        ElementId levelId;

                        if (firstEl.LevelId.IntegerValue == -1)
                        {
                            FilteredElementCollector lcol = new FilteredElementCollector(doc);
                            var randomLvl = lcol.OfClass(typeof(Level)).ToElements().LastOrDefault(); //Select random levelid
                            levelId = randomLvl.Id;
                        }
                        else
                        {
                            levelId = firstEl.LevelId;
                        }

                        tx.Start("Create pipe!");
                        Pipe.Create(doc, pipeType.Id, levelId, firstCon, secondCon);
                        tx.Commit();
                    }
                }
                else
                {
                    //if (elIds.Count == 0 || elIds.Count > 1) throw new Exception("Only works on single element! No or multiple elements selected!");
                    ElementId id = elIds.FirstOrDefault();
                    if (id == null)
                    {
                        throw new Exception("Getting element from selection failed!");
                    }
                    Element element = doc.GetElement(id);
                    var     cons    = mp.GetALLConnectorsFromElements(element);

                    //Get the typeId of the selected or read PipeType
                    var filter = fi.ParameterValueGenericFilter(doc, pipeTypeName, BuiltInParameter.ALL_MODEL_TYPE_NAME);
                    FilteredElementCollector col = new FilteredElementCollector(doc);
                    var pipeType = col.OfClass(typeof(PipeType)).WherePasses(filter).ToElements().FirstOrDefault();
                    if (pipeType == null)
                    {
                        throw new Exception("Collection of PipeType failed!");
                    }

                    //LevelId can be null -> work around
                    ElementId levelId;

                    if (element.LevelId.IntegerValue == -1)
                    {
                        FilteredElementCollector lcol = new FilteredElementCollector(doc);
                        var randomLvl = lcol.OfClass(typeof(Level)).ToElements().LastOrDefault(); //Select random levelid
                        levelId = randomLvl.Id;
                    }
                    else
                    {
                        levelId = element.LevelId;
                    }

                    Connector con = (from Connector c in cons where c.IsConnected == false select c).FirstOrDefault();
                    if (con == null)
                    {
                        throw new Exception("No not connected connectors in element!");
                    }

                    //If the element is a Pipe -> Create a bend in a specified direction
                    if (element is Pipe selectedPipe)
                    {
                        string bendDir;

                        //Get Pipe Size
                        double pipeSize = selectedPipe.Diameter;

                        //Select the direction to create in
                        BaseFormTableLayoutPanel_Basic ds = new BaseFormTableLayoutPanel_Basic(
                            System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, lad.Directions());
                        ds.ShowDialog();
                        bendDir = ds.strTR;

                        ElementId curPipingSysTypeId = selectedPipe.MEPSystem.GetTypeId();
                        ElementId curPipeTypeId      = selectedPipe.PipeType.Id;

                        XYZ iP       = con.Origin;
                        XYZ dirPoint = null;
                        //Create direction point
                        switch (bendDir)
                        {
                        case "Top":
                            dirPoint = new XYZ(iP.X, iP.Y, iP.Z + 5);
                            break;

                        case "Bottom":
                            dirPoint = new XYZ(iP.X, iP.Y, iP.Z - 5);
                            break;

                        case "Front":
                            dirPoint = new XYZ(iP.X, iP.Y - 5, iP.Z);
                            break;

                        case "Back":
                            dirPoint = new XYZ(iP.X, iP.Y + 5, iP.Z);
                            break;

                        case "Left":
                            dirPoint = new XYZ(iP.X - 5, iP.Y, iP.Z);
                            break;

                        case "Right":
                            dirPoint = new XYZ(iP.X + 5, iP.Y, iP.Z);
                            break;

                        default:
                            break;
                        }

                        using (TransactionGroup txGp = new TransactionGroup(doc))
                        {
                            txGp.Start("Create new elbow and pipe!");

                            Pipe newPipe;

                            using (Transaction tx1 = new Transaction(doc))
                            {
                                tx1.Start("Create new pipe!");

                                newPipe = Pipe.Create(doc, curPipingSysTypeId, curPipeTypeId, levelId, iP, dirPoint);

                                //Change size of the pipe
                                Parameter par = newPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
                                par.Set(pipeSize);

                                tx1.Commit();
                            }

                            //Find the connector from the dummy pipe at intersection
                            var       newCons = mp.GetALLConnectorsFromElements(newPipe);
                            Connector newCon  = newCons.Where(c => c.Equalz(con, Extensions._1mmTol)).FirstOrDefault();

                            using (Transaction tx2 = new Transaction(doc))
                            {
                                tx2.Start("Create new bend!");

                                doc.Create.NewElbowFitting(con, newCon);

                                tx2.Commit();
                            }

                            txGp.Assimilate();
                        }
                    }
                    else //if element is anything other than a Pipe -> Create a pipe from random connector
                    {
                        //Create a point in space to connect the pipe
                        XYZ direction    = con.CoordinateSystem.BasisZ.Multiply(2);
                        XYZ origin       = con.Origin;
                        XYZ pointInSpace = origin.Add(direction);

                        //Transaction that creates the pipe
                        Transaction tx = new Transaction(doc);
                        tx.Start("Create pipe!");
                        //Create the pipe
                        Pipe.Create(doc, pipeType.Id, levelId, con, pointInSpace);
                        tx.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return(Result.Succeeded);
        }