Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve project base point.
        /// </summary>
        bool GetBasePoint(
            Document doc,
            out XYZ basePoint,
            out double north)
        {
            BuiltInParameter[] bip = new[] {
                BuiltInParameter.BASEPOINT_EASTWEST_PARAM,
                BuiltInParameter.BASEPOINT_NORTHSOUTH_PARAM,
                BuiltInParameter.BASEPOINT_ELEVATION_PARAM,
                BuiltInParameter.BASEPOINT_ANGLETON_PARAM
            };

            FilteredElementCollector col
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(BasePoint));

            Parameter p = null;

            basePoint = null;
            north     = 0;

            foreach (BasePoint bp in col)
            {
                basePoint = new XYZ(
                    bp.get_Parameter(bip[0]).AsDouble(),
                    bp.get_Parameter(bip[1]).AsDouble(),
                    bp.get_Parameter(bip[2]).AsDouble());

                Debug.Print("base point {0}",
                            GeomVertices.PointString(basePoint));

                p = bp.get_Parameter(bip[3]);

                if (null != p)
                {
                    north = p.AsDouble();
                    Debug.Print("north {0}", north);
                    break;
                }
            }
            return(null != p);
        }
Ejemplo n.º 2
0
        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;

            // Useless initial attempt to retrieve project
            // location to calculate project location:
            //XYZ basePoint;
            //double north;
            //GetBasePoint( doc, out basePoint, out north );

            // Successful retrieval of active project
            // location position as a transform:

            Transform projectLocationTransform
                = GetProjectLocationTransform(doc);

            // Load or retrieve setout point family symbols:

            FamilySymbol[] symbols
                = GetFamilySymbols(doc, true);

            if (null == symbols)
            {
                message = string.Format(
                    "Unable to load setout point family from '{1}'.",
                    _family_path);

                return(Result.Failed);
            }

            // Retrieve structural concrete elements.

            FilteredElementCollector col
                = GetStructuralElements(doc);

            // Retrieve element geometry and place a
            // setout point on each geometry corner.

            // Setout points are numbered starting at
            // one each time the command is run with
            // no decoration or prefix whatsoever.

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Place Setout Points");

                Options opt = app.Create.NewGeometryOptions();

                // On the very first attempt only, run an error
                // check to see whether the required shared
                // parameters have actually been bound:

                bool first = true;

                foreach (Element e in col)
                {
                    Transform    t;
                    List <Solid> solids = GeomVertices.GetSolids(
                        e, opt, out t);

                    string desc = ElementDescription(e);

                    if (null == solids || 0 == solids.Count)
                    {
                        Debug.Print(
                            "Unable to access element solid for element {0}.",
                            desc);

                        continue;
                    }

                    Dictionary <XYZ, int> corners
                        = GeomVertices.GetCorners(solids);

                    int n = corners.Count;

                    Debug.Print("{0}: {1} corners found:",
                                desc, n);

                    foreach (XYZ p in corners.Keys)
                    {
                        ++_point_number;

                        Debug.Print("  {0}: {1}", _point_number,
                                    GeomVertices.PointString(p));

                        XYZ p1 = t.OfPoint(p);

                        // Handle error saying, "The symbol is not
                        // active.\r\nParameter name: symbol".

                        FamilyInstance fi
                            = doc.Create.NewFamilyInstance(p1,
                                                           symbols[1], StructuralType.NonStructural);

                        #region Test shared parameter availability
#if TEST_SHARED_PARAMETERS
                        // Test code to ensure that the shared
                        // parameters really are available

                        Parameter p1 = fi.get_Parameter("X");
                        Parameter p2 = fi.get_Parameter("Y");
                        Parameter p3 = fi.get_Parameter("Z");
                        Parameter p4 = fi.get_Parameter("Host_Geometry");
                        Parameter p5 = fi.get_Parameter("Point_Number");

                        //doc.Regenerate(); // no need for this, thankfully

                        //Parameter p11 = fi.get_Parameter(
                        //  "{7a5d1056-a1df-4389-b026-9f32fc3ac5fb}" );

                        //Parameter p12 = fi.get_Parameter(
                        //  "7a5d1056-a1df-4389-b026-9f32fc3ac5fb" );
#endif // TEST_SHARED_PARAMETERS
                        #endregion // Test shared parameter availability

                        // Add shared parameter data immediately
                        // after creating the new family instance.
                        // The shared parameters are indeed added
                        // immediately by Revit, so we can access and
                        // populate them.
                        // No need to commit the transaction that
                        // added the family instance to give Revit
                        // a chance to add the shared parameters to
                        // it, nor to regenerate the document, we
                        // can write the shared parameter values
                        // right away.

                        if (first)
                        {
                            Parameter q = fi.get_Parameter(
                                _parameter_x);

                            if (null == q)
                            {
                                message =
                                    "The required shared parameters "
                                    + "X, Y, Z, Host_Id, Host_Type and "
                                    + "Point_Number are missing.";

                                tx.RollBack();

                                return(Result.Failed);
                            }
                            first = false;
                        }

                        // Transform insertion point by applying
                        // base point offset, scaling from feet,
                        // and rotating to project north.
                        //XYZ r1 = ( p + basePoint) * _feetToMm;

                        // Transform insertion point by applying
                        // project location transformation.

                        XYZ r2 = projectLocationTransform.OfPoint(p);

                        fi.get_Parameter(_parameter_host_type).Set(
                            GetHostType(e).ToString());

                        fi.get_Parameter(_parameter_host_id).Set(
                            e.Id.IntegerValue);

                        fi.get_Parameter(_parameter_point_nr).Set(
                            _point_number.ToString());

                        fi.get_Parameter(_parameter_x).Set(r2.X);
                        fi.get_Parameter(_parameter_y).Set(r2.Y);
                        fi.get_Parameter(_parameter_z).Set(r2.Z);
                    }
                }

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