示例#1
0
        /// <summary>
        /// Return DWF links
        /// </summary>
        //FilteredElementCollector GetDwfLinks(
        //  Document doc )
        //{
        //  // http://forums.autodesk.com/t5/revit-api/get-all-linked-dwfx-files-from-in-revit-document/td-p/5769622

        //  return Util.GetElementsOfType( doc,
        //    typeof( ImportInstance ) );
        //}

        FilteredElementCollector GetLinkedFiles(
            Document doc)
        {
            return(Util.GetElementsOfType(doc,
                                          typeof(Instance),
                                          BuiltInCategory.OST_RvtLinks));
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

            FilteredElementCollector levels = Util.GetElementsOfType(
                doc, typeof(Level), BuiltInCategory.OST_Levels);

            Level level = levels.FirstElement() as Level;

            PlanTopology pt = doc.get_PlanTopology(level);

            // Collect some data on each room in the circuit

            string output = "Rooms on "
                            + level.Name + ":"
                            + "\n  Name and Number : Area";

            //foreach( Room r in pt.Rooms ) // 2012

            foreach (ElementId id in pt.GetRoomIds()) // 2013
            {
                Room r = doc.GetElement(id) as Room;

                output += "\n  " + r.Name + " : "
                          + Util.RealString(r.Area) + " sqf";
            }
            Util.InfoMsg(output);

            output = "Circuits without rooms:"
                     + "\n  Number of Sides : Area";

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Create New Rooms");

                foreach (PlanCircuit pc in pt.Circuits)
                {
                    if (!pc.IsRoomLocated) // this circuit has no room, create one
                    {
                        output += "\n  " + pc.SideNum + " : "
                                  + Util.RealString(pc.Area) + " sqf";

                        // Pass null to create a new room;
                        // to place an existing unplaced room,
                        // pass it in instead of null:

                        Room r = doc.Create.NewRoom(null, pc);
                    }
                }
                t.Commit();
            }
            Util.InfoMsg(output);

            return(Result.Succeeded);
        }
        Category GetCategory(
            Document doc,
            BuiltInCategory target)
        {
            Category cat = null;

            if (target.Equals(BuiltInCategory.OST_IOSModelGroups))
            {
                // determine model group category:

                FilteredElementCollector collector
                    = Util.GetElementsOfType(doc, typeof(Group), // GroupType works as well
                                             BuiltInCategory.OST_IOSModelGroups);

                IList <Element> modelGroups = collector.ToElements();

                if (0 == modelGroups.Count)
                {
                    Util.ErrorMsg("Please insert a model group.");
                    return(cat);
                }
                else
                {
                    cat = modelGroups[0].Category;
                }
            }
            else
            {
                try
                {
                    cat = doc.Settings.Categories.get_Item(target);
                }
                catch (Exception ex)
                {
                    Util.ErrorMsg(string.Format(
                                      "Error obtaining document {0} category: {1}",
                                      target.ToString(), ex.Message));
                    return(cat);
                }
            }
            if (null == cat)
            {
                Util.ErrorMsg(string.Format(
                                  "Unable to obtain the document {0} category.",
                                  target.ToString()));
            }
            return(cat);
        }
示例#4
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app   = commandData.Application;
            UIDocument    uidoc = app.ActiveUIDocument;
            Document      doc   = uidoc.Document;

            //Autodesk.Revit.Creation.Application creApp = app.Application.Create;
            //Autodesk.Revit.Creation.Document creDoc = doc.Create;

            FilteredElementCollector doors
                = Util.GetElementsOfType(doc,
                                         typeof(FamilyInstance),
                                         BuiltInCategory.OST_Doors);

            int n = doors.Count();

            Debug.Print("{0} door{1} found.",
                        n, Util.PluralSuffix(n));

            if (0 < n)
            {
                Dictionary <string, List <Element> > marks
                    = new Dictionary <string, List <Element> >();

                foreach (FamilyInstance door in doors)
                {
                    string mark = door.get_Parameter(
                        BuiltInParameter.ALL_MODEL_MARK)
                                  .AsString();

                    if (!marks.ContainsKey(mark))
                    {
                        marks.Add(mark, new List <Element>());
                    }
                    marks[mark].Add(door);
                }

                List <string> keys = new List <string>(
                    marks.Keys);

                keys.Sort();

                n = keys.Count;

                Debug.Print("{0} door mark{1} found{2}",
                            n, Util.PluralSuffix(n),
                            Util.DotOrColon(n));

                foreach (string mark in keys)
                {
                    n = marks[mark].Count;

                    Debug.Print("  {0}: {1} door{2}",
                                mark, n, Util.PluralSuffix(n));
                }
            }

            n = 0; // count how many elements are modified

            if (_modify_existing_marks)
            {
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Modify Existing Door Marks");

                    //ElementSet els = uidoc.Selection.Elements; // 2014

                    ICollection <ElementId> ids = uidoc.Selection
                                                  .GetElementIds(); // 2015

                    //foreach( Element e in els ) // 2014

                    foreach (ElementId id in ids)       // 2015
                    {
                        Element e = doc.GetElement(id); // 2015

                        if (e is FamilyInstance &&
                            null != e.Category &&
                            (int)BuiltInCategory.OST_Doors
                            == e.Category.Id.IntegerValue)
                        {
                            e.get_Parameter(
                                BuiltInParameter.ALL_MODEL_MARK)
                            .Set(_the_answer);

                            ++n;
                        }
                    }
                    tx.Commit();
                }
            }

            // return Succeeded only if we wish to commit
            // the transaction to modify the database:
            //
            //return 0 < n
            //  ? Result.Succeeded
            //  : Result.Failed;
            //
            // That was only useful before the introduction
            // of the manual and read-only transaction modes.

            return(Result.Succeeded);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            Debug.Assert(false, "This has not been tested since Revit 2010!");

            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

            Autodesk.Revit.Creation.Application ca
                = app.Application.Create;

            Autodesk.Revit.Creation.Document cd
                = doc.Create;

            // determine line load symbol to use:

            FilteredElementCollector symbols
                = new FilteredElementCollector(doc);

            symbols.OfClass(typeof(LineLoadType));

            LineLoadType loadSymbol
                = symbols.FirstElement() as LineLoadType;

            // sketch plane and arrays of forces and moments:

            //Plane plane = ca.NewPlane( XYZ.BasisZ, XYZ.Zero ); // 2016
            Plane plane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero); // 2017

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Create New Line Load");

                //SketchPlane skplane = cd.NewSketchPlane( plane ); // 2013

                SketchPlane skplane = SketchPlane.Create(doc, plane); // 2014

                XYZ        forceA = new XYZ(0, 0, 5);
                XYZ        forceB = new XYZ(0, 0, 10);
                List <XYZ> forces = new List <XYZ>();
                forces.Add(forceA);
                forces.Add(forceB);

                XYZ        momentA = new XYZ(0, 0, 0);
                XYZ        momentB = new XYZ(0, 0, 0);
                List <XYZ> moments = new List <XYZ>();
                moments.Add(momentA);
                moments.Add(momentB);

                BuiltInCategory bic
                    = BuiltInCategory.OST_StructuralFraming;

                FilteredElementCollector beams = Util.GetElementsOfType(
                    doc, typeof(FamilyInstance), bic);

                XYZ p1 = new XYZ(0, 0, 0);
                XYZ p2 = new XYZ(3, 0, 0);
                //List<XYZ> points = new List<XYZ>();
                //points.Add( p1 );
                //points.Add( p2 );

                // create a new unhosted line load on points:

                //LineLoad lineLoadNoHost = cd.NewLineLoad(
                //  points, forces, moments,
                //  false, false, false,
                //  loadSymbol, skplane ); // 2015

                LineLoad lineLoadNoHost = LineLoad.Create(doc,
                                                          p1, p2, forces[0], moments[0],
                                                          loadSymbol, skplane); // 2016

                Debug.Print("Unhosted line load works.");

                // create new line loads on beam:

                foreach (Element e in beams)
                {
                    try
                    {
                        //LineLoad lineLoad = cd.NewLineLoad(
                        //  e, forces, moments,
                        //  false, false, false,
                        //  loadSymbol, skplane ); // 2015

                        AnalyticalModelSurface amsurf = e.GetAnalyticalModel()
                                                        as AnalyticalModelSurface;

                        LineLoad lineLoad = LineLoad.Create(doc,
                                                            amsurf, 0, forces[0], moments[0], loadSymbol); // 2016

                        Debug.Print("Hosted line load on beam works.");
                    }
                    catch (Exception ex)
                    {
                        Debug.Print("Hosted line load on beam fails: "
                                    + ex.Message);
                    }

                    FamilyInstance i = e as FamilyInstance;

                    AnalyticalModel am = i.GetAnalyticalModel();

                    foreach (Curve curve in
                             am.GetCurves(AnalyticalCurveType.ActiveCurves))
                    {
                        try
                        {
                            //LineLoad lineLoad = cd.NewLineLoad(
                            //  curve.Reference, forces, moments,
                            //  false, false, false,
                            //  loadSymbol, skplane ); // 2015

                            AnalyticalModelStick amstick = e.GetAnalyticalModel()
                                                           as AnalyticalModelStick;

                            LineLoad lineLoad = LineLoad.Create(doc,
                                                                amstick, forces[0], moments[0], loadSymbol); // 2016

                            Debug.Print("Hosted line load on "
                                        + "AnalyticalModelFrame curve works.");
                        }
                        catch (Exception ex)
                        {
                            Debug.Print("Hosted line load on "
                                        + "AnalyticalModelFrame curve fails: "
                                        + ex.Message);
                        }
                    }
                }
                t.Commit();
            }
            return(Result.Succeeded);
        }
示例#6
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet highlightElements)
        {
            /*
             *
             * // retrieve all link elements:
             *
             * Document doc = app.ActiveUIDocument.Document;
             * List<Element> links = GetElements(
             * BuiltInCategory.OST_RvtLinks,
             * typeof( Instance ), app, doc );
             *
             * // determine the link paths:
             *
             * DocumentSet docs = app.Documents;
             * int n = docs.Size;
             * Dictionary<string, string> paths
             * = new Dictionary<string, string>( n );
             *
             * foreach( Document d in docs )
             * {
             * string path = d.PathName;
             * int i = path.LastIndexOf( "\\" ) + 1;
             * string name = path.Substring( i );
             * paths.Add( name, path );
             * }
             */

            // Retrieve lighting fixture element
            // data from linked documents:

            List <ElementData> data = new List <ElementData>();
            UIApplication      app  = commandData.Application;
            DocumentSet        docs = app.Application.Documents;

            foreach (Document doc in docs)
            {
                FilteredElementCollector a
                    = Util.GetElementsOfType(doc,
                                             typeof(FamilyInstance),
                                             BuiltInCategory.OST_LightingFixtures);

                foreach (FamilyInstance e in a)
                {
                    string        name = e.Name;
                    LocationPoint lp   = e.Location as LocationPoint;
                    if (null != lp)
                    {
                        XYZ p = lp.Point;
                        data.Add(new ElementData(doc.PathName, e.Name,
                                                 e.Id.IntegerValue, p.X, p.Y, p.Z, e.UniqueId));
                    }
                }
            }

            // Display data:

            using (CmdLinkedFileElementsForm dlg = new CmdLinkedFileElementsForm(data))
            {
                dlg.ShowDialog();
            }

            return(Result.Succeeded);
        }
        /*
         * public Result Execute2(
         * ExternalCommandData commandData,
         * ref string messages,
         * ElementSet elements )
         * {
         * UIApplication app = commandData.Application;
         * Document doc = app.ActiveUIDocument.Document;
         * CreationFilter cf = app.Create.Filter;
         *
         * Filter f1 = cf.NewParameterFilter(
         *  BuiltInParameter.DESIGN_OPTION_PARAM,
         *  CriteriaFilterType.Equal, "Main Model" );
         *
         * Filter f2 = cf.NewTypeFilter( typeof( Wall ) );
         * Filter f = cf.NewLogicAndFilter( f1, f2 );
         *
         * List<Element> a = new List<Element>();
         *
         * doc.get_Elements( f, a );
         *
         * Util.InfoMsg( "There are "
         + a.Count.ToString()
         + " main model wall elements" );
         +
         + return Result.Succeeded;
         + }
         */

        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

            // this returns zero symbols:
            //BuiltInCategory bic = BuiltInCategory.OST_StairsRailing;
            //Filter f1 = cf.NewCategoryFilter( bic );
            //Filter f2 = cf.NewTypeFilter( typeof( FamilySymbol ) );
            //Filter f = cf.NewLogicAndFilter( f1, f2 );

            // this returns zero families:
            // we already know why, because family does not implement the Category property, c.f.
            // http://thebuildingcoder.typepad.com/blog/2009/01/family-category-and-filtering.html
            //Filter f1 = cf.NewCategoryFilter( bic );
            //Filter f2 = cf.NewTypeFilter( typeof( Family ) );
            //Filter f = cf.NewLogicAndFilter( f1, f2 );

            BuiltInCategory bic
                = BuiltInCategory.OST_StairsRailingBaluster;

            IList <Element> symbols = Util.GetElementsOfType(
                doc, typeof(FamilySymbol), bic).ToElements();

            int n = symbols.Count;

            Debug.Print("\n{0}"
                        + " OST_StairsRailingBaluster"
                        + " family symbol{1}:",
                        n, Util.PluralSuffix(n));

            foreach (FamilySymbol s in symbols)
            {
                Debug.Print(
                    "Family name={0}, symbol name={1}",
                    s.Family.Name, s.Name);
            }

            bic = BuiltInCategory.OST_StairsRailing;

            symbols = Util.GetElementsOfType(
                doc, typeof(ElementType), bic).ToElements();

            n = symbols.Count;

            Debug.Print("\n{0}"
                        + " OST_StairsRailing symbol{1}:",
                        n, Util.PluralSuffix(n));

            foreach (ElementType s in symbols)
            {
                FamilySymbol fs = s as FamilySymbol;

                Debug.Print(
                    "Family name={0}, symbol name={1}",
                    null == fs ? "<none>" : fs.Family.Name,
                    s.Name);
            }
            return(Result.Failed);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

            FilteredElementCollector levels = Util.GetElementsOfType(
                doc, typeof(Level), BuiltInCategory.OST_Levels);

            Level level = levels.FirstElement() as Level;

            if (null == level)
            {
                message = "No level found.";
                return(Result.Failed);
            }

            // get symbol to use:

            BuiltInCategory bic;
            Type            t;

            // this retrieves the railing baluster symbols
            // but they cannot be used to create a railing:

            bic = BuiltInCategory.OST_StairsRailingBaluster;
            t   = typeof(FamilySymbol);

            // this retrieves all railing symbols,
            // but they are just Symbol instances,
            // not FamilySymbol ones:

            bic = BuiltInCategory.OST_StairsRailing;
            t   = typeof(ElementType);

            FilteredElementCollector symbols
                = Util.GetElementsOfType(doc, t, bic);

            FamilySymbol sym = null;

            foreach (ElementType s in symbols)
            {
                FamilySymbol fs = s as FamilySymbol;

                Debug.Print(
                    "Family name={0}, symbol name={1},"
                    + " category={2}",
                    null == fs ? "<none>" : fs.Family.Name,
                    s.Name,
                    s.Category.Name);

                if (null == sym && s is ElementType)
                {
                    // this does not work, of course:
                    sym = s as FamilySymbol;
                }
            }
            if (null == sym)
            {
                message = "No railing family symbols found.";
                return(Result.Failed);
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Create New Railing");

                XYZ  p1   = new XYZ(17, 0, 0);
                XYZ  p2   = new XYZ(33, 0, 0);
                Line line = Line.CreateBound(p1, p2);

                // we need a FamilySymbol instance here, but only have a Symbol:

                FamilyInstance Railing1
                    = doc.Create.NewFamilyInstance(
                          line, sym, level, StructuralType.NonStructural);

                tx.Commit();
            }
            return(Result.Succeeded);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            // Get a lighting fixture family symbol:

            FilteredElementCollector symbols
                = Util.GetElementsOfType(doc,
                                         typeof(FamilySymbol),
                                         BuiltInCategory.OST_LightingFixtures);

            FamilySymbol sym = symbols.FirstElement()
                               as FamilySymbol;

            if (null == sym)
            {
                message = "No lighting fixture symbol found.";
                return(Result.Failed);
            }

            // Pick the ceiling:

#if _2010
            uidoc.Selection.StatusbarTip
                = "Please select ceiling to host lighting fixture";

            uidoc.Selection.PickOne();

            Element ceiling = null;

            foreach (Element e in uidoc.Selection.Elements)
            {
                ceiling = e as Element;
                break;
            }
#endif // _2010

            Reference r = uidoc.Selection.PickObject(
                ObjectType.Element,
                "Please select ceiling to host lighting fixture");

            if (null == r)
            {
                message = "Nothing selected.";
                return(Result.Failed);
            }

            // 'Autodesk.Revit.DB.Reference.Element' is
            // obsolete: Property will be removed. Use
            // Document.GetElement(Reference) instead.
            //Element ceiling = r.Element; // 2011

            Element ceiling = doc.GetElement(r) as Wall; // 2012

            // Get the level 1:

            Level level = Util.GetFirstElementOfTypeNamed(
                doc, typeof(Level), "Level 1") as Level;

            if (null == level)
            {
                message = "Level 1 not found.";
                return(Result.Failed);
            }

            // Create the family instance:

            XYZ p = app.Create.NewXYZ(-43, 28, 0);

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Place New Lighting Fixture Instance");

                FamilyInstance instLight
                    = doc.Create.NewFamilyInstance(
                          p, sym, ceiling, level,
                          StructuralType.NonStructural);

                t.Commit();
            }
            return(Result.Succeeded);
        }