Example #1
0
        public void DrawGroup(CurveArray curves, string name)
        {
            ModelCurveArray roomShape = MakeModelCurve(curves);

            Autodesk.Revit.DB.Group group = _doc.Create.NewGroup(roomShape.Cast <ModelCurve>().Select(x => x.Id).ToList());
            group.GroupType.Name = name;
        }
Example #2
0
        public ModelCurveArray MakeModelCurve(BoundingBoxXYZ bb)
        {
            ModelCurveArray array = new ModelCurveArray();
            XYZ             p1    = bb.Min;
            XYZ             p2    = bb.Max;

            XYZ[][] pairs =
            {
                new XYZ[] { new XYZ(p1.X, p1.Y, p1.Z), new XYZ(p2.X, p1.Y, p1.Z) },
                new XYZ[] { new XYZ(p2.X, p2.Y, p1.Z), new XYZ(p2.X, p1.Y, p1.Z) },
                new XYZ[] { new XYZ(p2.X, p2.Y, p1.Z), new XYZ(p1.X, p2.Y, p1.Z) },
                new XYZ[] { new XYZ(p1.X, p1.Y, p1.Z), new XYZ(p1.X, p2.Y, p1.Z) },

                new XYZ[] { new XYZ(p1.X, p1.Y, p2.Z), new XYZ(p2.X, p1.Y, p2.Z) },
                new XYZ[] { new XYZ(p2.X, p2.Y, p2.Z), new XYZ(p2.X, p1.Y, p2.Z) },
                new XYZ[] { new XYZ(p2.X, p2.Y, p2.Z), new XYZ(p1.X, p2.Y, p2.Z) },
                new XYZ[] { new XYZ(p1.X, p1.Y, p2.Z), new XYZ(p1.X, p2.Y, p2.Z) },

                new XYZ[] { new XYZ(p1.X, p1.Y, p1.Z), new XYZ(p1.X, p1.Y, p2.Z) },
                new XYZ[] { new XYZ(p2.X, p1.Y, p1.Z), new XYZ(p2.X, p1.Y, p2.Z) },
                new XYZ[] { new XYZ(p2.X, p2.Y, p1.Z), new XYZ(p2.X, p2.Y, p2.Z) },
                new XYZ[] { new XYZ(p1.X, p2.Y, p1.Z), new XYZ(p1.X, p2.Y, p2.Z) }
            };

            foreach (var pair in pairs)
            {
                ModelCurve modelCurve = MakeModelCurve(pair[0], pair[1]);
                array.Append(modelCurve);
            }
            return(array);
        }
Example #3
0
        private static Element CreateRoof()
        {
            var curveArray      = CreateCurveArray();
            var modelCurveArray = new ModelCurveArray();
            var roofType        = new FilteredElementCollector(Loader.Document).OfClass(typeof(RoofType)).FirstOrDefault() as RoofType;
            var roof            = Loader.Document.Create.NewFootPrintRoof(curveArray, Loader.Level, roofType, out modelCurveArray);

            return(roof);
        }
Example #4
0
        /// <summary>
        /// 在指定的平面上绘制一组模型线
        /// </summary>
        /// <param name="tranDoc"></param>
        /// <param name="doc"></param>
        /// <param name="curves">请自行确保集合中的所有曲线都在同一个平面上</param>
        /// <param name="sp"></param>
        /// <returns></returns>
        public static ModelCurveArray DrawModelCurves(Transaction tranDoc, Document doc, IEnumerable <Curve> curves, SketchPlane sp)
        {
            CurveArray ca;

            Curves.CurvesConverter.Convert(curves, out ca);
            ModelCurveArray mca = doc.Create.NewModelCurveArray(ca, sp);

            return(mca);
        }
Example #5
0
        void CommitInstance
        (
            Document doc, IGH_DataAccess DA, int Iteration,
            Rhino.Geometry.Curve boundary,
            Autodesk.Revit.DB.RoofType roofType,
            Autodesk.Revit.DB.Level level
        )
        {
            var element = PreviousElement(doc, Iteration);

            if (!element?.Pinned ?? false)
            {
                ReplaceElement(doc, DA, Iteration, element);
            }
            else
            {
                try
                {
                    var scaleFactor = 1.0 / Revit.ModelUnits;
                    if (scaleFactor != 1.0)
                    {
                        boundary?.Scale(scaleFactor);
                    }

                    if
                    (
                        boundary == null ||
                        boundary.IsShort(Revit.ShortCurveTolerance) ||
                        !boundary.IsClosed ||
                        !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) ||
                        boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
                    )
                    {
                        throw new Exception(string.Format("Parameter '{0}' must be an horizontal planar closed curve.", Params.Input[0].Name));
                    }

                    var curveArray = boundary.ToHost().ToCurveArray();
                    var footPintToModelCurvesMapping = new ModelCurveArray();
                    element = CopyParametersFrom(doc.Create.NewFootPrintRoof(curveArray, level, roofType, out footPintToModelCurvesMapping), element);

                    if (element != null)
                    {
                        var boundaryBBox = boundary.GetBoundingBox(true);
                        element.get_Parameter(BuiltInParameter.ROOF_BASE_LEVEL_PARAM).Set(level.Id);
                        element.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM).Set(boundaryBBox.Min.Z - level.Elevation);
                    }

                    ReplaceElement(doc, DA, Iteration, element);
                }
                catch (Exception e)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
                    ReplaceElement(doc, DA, Iteration, null);
                }
            }
        }
Example #6
0
        public ModelCurveArray MakeModelCurve(IEnumerable <Curve> curves)
        {
            ModelCurveArray array = new ModelCurveArray();

            foreach (Curve curve in curves)
            {
                ModelCurve modelCurve = MakeModelCurve(curve);
                array.Append(modelCurve);
            }
            return(array);
        }
Example #7
0
        public ModelCurveArray MakeModelCurve(CurveArray arr)
        {
            ModelCurveArray array = new ModelCurveArray();

            foreach (Curve c in arr)
            {
                ModelCurve modelCurve = MakeModelCurve(c);
                array.Append(modelCurve);
            }
            return(array);
        }
Example #8
0
        public ModelCurveArray CreateModelCurves(
            Curve curve)
        {
            var array = new ModelCurveArray();

            var line = curve as Line;

            if (line != null)
            {
                array.Append(CreateModelLine(_doc,
                                             curve.GetEndPoint(0),
                                             curve.GetEndPoint(1)));

                return(array);
            }

            var arc = curve as Arc;

            if (arc != null)
            {
                var origin = arc.Center;
                var normal = arc.Normal;

                array.Append(CreateModelCurve(
                                 arc, origin, normal));

                return(array);
            }

            var ellipse = curve as Ellipse;

            if (ellipse != null)
            {
                var origin = ellipse.Center;
                var normal = ellipse.Normal;

                array.Append(CreateModelCurve(
                                 ellipse, origin, normal));

                return(array);
            }

            var points = curve.Tessellate();
            var p      = points.First();

            foreach (var q in points.Skip(1))
            {
                array.Append(CreateModelLine(_doc, p, q));
                p = q;
            }

            return(array);
        }
Example #9
0
        void ReconstructRoofByOutline
        (
            Document doc,
            ref Autodesk.Revit.DB.Element element,

            Rhino.Geometry.Curve boundary,
            Optional <Autodesk.Revit.DB.RoofType> type,
            Optional <Autodesk.Revit.DB.Level> level
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                scaleFactor != 1.0 ? !boundary.Scale(scaleFactor) : true &&
                boundary.IsShort(Revit.ShortCurveTolerance) ||
                !boundary.IsClosed ||
                !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) ||
                boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
            )
            {
                ThrowArgumentException(nameof(boundary), "Boundary must be an horizontal planar closed curve.");
            }

            SolveOptionalType(ref type, doc, ElementTypeGroup.RoofType, nameof(type));

            double minZ = boundary.GetBoundingBox(true).Min.Z;

            SolveOptionalLevel(ref level, doc, minZ, nameof(level));

            var parametersMask = new BuiltInParameter[]
            {
                BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                BuiltInParameter.ELEM_FAMILY_PARAM,
                BuiltInParameter.ELEM_TYPE_PARAM,
                BuiltInParameter.LEVEL_PARAM,
                BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM
            };

            using (var curveArray = boundary.ToHost().ToCurveArray())
            {
                var modelCurveArray = new ModelCurveArray();
                ReplaceElement(ref element, doc.Create.NewFootPrintRoof(curveArray, level.Value, type.Value, out modelCurveArray), parametersMask);
            }

            if (element != null)
            {
                element.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM).Set(minZ - level.Value.Elevation);
            }
        }
Example #10
0
        void SetModelCurvesColor(
            ModelCurveArray modelCurves,
            View view,
            Color color)
        {
            foreach (var curve in modelCurves
                     .Cast <ModelCurve>())
            {
                var overrides = view.GetElementOverrides(
                    curve.Id);

                overrides.SetProjectionLineColor(color);

                view.SetElementOverrides(curve.Id, overrides);
            }
        }
Example #11
0
        /// <summary>
        /// Creates a RoofFootPrint.
        /// </summary>
        /// <param name="overhang">The overhang value.</param>
        /// <param name="slope">The slope of the roof.</param>
        /// <param name="slopeDirection">The direction of the slope.</param>
        /// <param name="footPrint">The footprint.</param>
        /// <returns>
        /// Returns a reference of the roof generated.
        /// </returns>
        private FootPrintRoof CreateFootPrintRoof(double overhang, double slope, XYZ slopeDirection, CurveArray footPrint)
        {
            // get a roof type
            FilteredElementCollector collector = new FilteredElementCollector(document);

            collector.OfClass(typeof(RoofType));
            RoofType roofType = collector.FirstElement() as RoofType;

            // create the foot print of the roof
            ModelCurveArray footPrintToModelCurveMapping = new ModelCurveArray();
            FootPrintRoof   footPrintRoof = document.Create.NewFootPrintRoof(footPrint, roofLevel, roofType, out footPrintToModelCurveMapping);

            // create the slope
            ApplySlope(overhang, slope, slopeDirection, footPrintRoof, footPrintToModelCurveMapping);

            return(footPrintRoof);
        }
Example #12
0
 private void CreateModelCurveArray(CurveArray curveArray, XYZ normal, XYZ point)
 {
     if (curveArray.Size > 0)
     {
         Transaction transaction2 = new Transaction(doc);
         transaction2.Start("绘制模型线");
         try
         {
             SketchPlane     modelSketch = SketchPlane.Create(doc, app.Create.NewPlane(normal, point));
             ModelCurveArray modelLine   = doc.Create.NewModelCurveArray(curveArray, modelSketch);
         }
         catch
         {
         }
         transaction2.Commit();
         curveArray.Clear();
     }
 }
Example #13
0
        /// <summary>
        /// Create a footprint roof.
        /// </summary>
        /// <param name="footPrint">The footprint is a curve loop, or a wall loop, or loops combined of walls and curves</param>
        /// <param name="level">The base level of the roof to be created.</param>
        /// <param name="roofType">The type of the newly created roof.</param>
        /// <returns>Return a new created footprint roof.</returns>
        public FootPrintRoof CreateFootPrintRoof(CurveArray footPrint, Level level, RoofType roofType)
        {
            FootPrintRoof footprintRoof = null;
            Transaction createRoofTransaction = new Transaction(m_commandData.Application.ActiveUIDocument.Document, "FootPrintRoof");
            createRoofTransaction.Start();
            try
            {
                ModelCurveArray footPrintToModelCurveMapping = new ModelCurveArray();
                footprintRoof = m_creationDoc.NewFootPrintRoof(footPrint, level, roofType, out footPrintToModelCurveMapping);
                createRoofTransaction.Commit();
            }
            catch (System.Exception e)
            {
                createRoofTransaction.RollBack();
                throw e;
            }

            return footprintRoof;
        }
Example #14
0
        /// <summary>
        /// Create a footprint roof.
        /// </summary>
        /// <param name="footPrint">The footprint is a curve loop, or a wall loop, or loops combined of walls and curves</param>
        /// <param name="level">The base level of the roof to be created.</param>
        /// <param name="roofType">The type of the newly created roof.</param>
        /// <returns>Return a new created footprint roof.</returns>
        public FootPrintRoof CreateFootPrintRoof(CurveArray footPrint, Level level, RoofType roofType)
        {
            FootPrintRoof footprintRoof         = null;
            Transaction   createRoofTransaction = new Transaction(m_commandData.Application.ActiveUIDocument.Document, "FootPrintRoof");

            createRoofTransaction.Start();
            try
            {
                ModelCurveArray footPrintToModelCurveMapping = new ModelCurveArray();
                footprintRoof = m_creationDoc.NewFootPrintRoof(footPrint, level, roofType, out footPrintToModelCurveMapping);
                createRoofTransaction.Commit();
            }
            catch (System.Exception e)
            {
                createRoofTransaction.RollBack();
                throw e;
            }

            return(footprintRoof);
        }
        /// <summary>
        /// The default constructor
        /// </summary>
        /// <param name="revit">The reference of the application in revit</param>
        public ModelLines(Autodesk.Revit.UI.UIApplication revit)
        {
            // Store the reference of the application for further use.
            m_revit = revit;
            // Get the create references
            m_createApp = m_revit.Application.Create;               // Creation.Application
            m_createDoc = m_revit.ActiveUIDocument.Document.Create; // Creation.Document

            // Construct all the ModelCurveArray instances for model lines
            m_lineArray    = new ModelCurveArray();
            m_arcArray     = new ModelCurveArray();
            m_ellipseArray = new ModelCurveArray();
            m_hermiteArray = new ModelCurveArray();
            m_nurbArray    = new ModelCurveArray();

            // Construct the sketch plane list data
            m_sketchArray = new List <SketchPlane>();

            // Construct the information list data
            m_informationMap = new List <ModelCurveCounter>();
        }
Example #16
0
        List<SketchPlane> m_sketchArray; // Store the SketchPlane references

        #endregion Fields

        #region Constructors

        /// <summary>
        /// The default constructor
        /// </summary>
        /// <param name="revit">The reference of the application in revit</param>
        public ModelLines(Autodesk.Revit.UI.UIApplication revit)
        {
            // Store the reference of the application for further use.
            m_revit = revit;
            // Get the create references
            m_createApp = m_revit.Application.Create;       // Creation.Application
            m_createDoc = m_revit.ActiveUIDocument.Document.Create;// Creation.Document

            // Construct all the ModelCurveArray instances for model lines
            m_lineArray = new ModelCurveArray();
            m_arcArray = new ModelCurveArray();
            m_ellipseArray = new ModelCurveArray();
            m_hermiteArray = new ModelCurveArray();
            m_nurbArray = new ModelCurveArray();

            // Construct the sketch plane list data
            m_sketchArray = new List<SketchPlane>();

            // Construct the information list data
            m_informationMap = new List<ModelCurveCounter>();
        }
Example #17
0
        private void CreateHiddenButterflyRoof(CurveArray footPrint, double slope, XYZ slopeDirection)
        {
            Polygon     roofPolygon = new Polygon(footPrint);
            List <Line> cutLines    = new List <Line>();

            XYZ divisionDirection = new XYZ(-slopeDirection.Y, slopeDirection.X, 0);

            List <CurveArray> convexFootPrint = roofPolygon.DividePolygonInHalf(divisionDirection, out Line cutLine);

            cutLines.Add(cutLine);

            foreach (CurveArray curveArray in convexFootPrint)
            {
                // get a roof type
                FilteredElementCollector collector = new FilteredElementCollector(document);
                collector.OfClass(typeof(RoofType));
                RoofType roofType = collector.FirstElement() as RoofType;

                // create the foot print of the roof
                ModelCurveArray footPrintToModelCurveMapping = new ModelCurveArray();
                FootPrintRoof   footPrintRoof = document.Create.NewFootPrintRoof(curveArray, roofLevel, roofType, out footPrintToModelCurveMapping);

                // apply the slope for the cutLines
                ModelCurveArrayIterator iterator = footPrintToModelCurveMapping.ForwardIterator();
                iterator.Reset();
                while (iterator.MoveNext())
                {
                    ModelCurve modelCurve = iterator.Current as ModelCurve;
                    Curve      curve      = modelCurve.GeometryCurve;

                    if (VerifyIntersectionInArray(curve, cutLines))
                    {
                        footPrintRoof.set_DefinesSlope(modelCurve, true);
                        footPrintRoof.set_SlopeAngle(modelCurve, slope);
                    }
                }
            }
            CreateParapetWall(footPrint);
        }
Example #18
0
        /// <summary>
        /// Initialize a roof element
        /// </summary>
        private void InitRoof(CurveArray curves, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.RoofType roofType)
        {
            TransactionManager.Instance.EnsureInTransaction(Document);

            Autodesk.Revit.DB.RoofBase roof = ElementBinder.GetElementFromTrace <Autodesk.Revit.DB.RoofBase>(Document);
            if (roof == null)
            {
                ModelCurveArray footprint = new ModelCurveArray();
                roof = Document.Create.NewFootPrintRoof(curves, level, roofType, out footprint);
            }
            else
            {
                ModelCurveArray footprint = new ModelCurveArray();
                roof = Document.Create.NewFootPrintRoof(curves, level, roofType, out footprint);
            }

            InternalSetRoof(roof);

            TransactionManager.Instance.TransactionTaskDone();

            ElementBinder.CleanupAndSetElementForTrace(Document, InternalRoof);
        }
Example #19
0
        public void addRoofs()
        {
            RoofType roofType
                = new FilteredElementCollector(activeDocument)
                  .OfClass(typeof(RoofType))
                  .First <Element>(
                      e => e.Name.Equals("Roof 1")) //change to roof type we want in end (TODO)
                  as RoofType;

            double roofElevation = useableRooms[0].GetARoom()[0].get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble(); //gets elevation from wall height

            using (Transaction t = new Transaction(activeDocument))
            {
                t.Start("Roof adding");
                Level level = Level.Create(activeDocument, roofElevation);
                foreach (Room toRoof in useableRooms)
                {
                    ModelCurveArray footPrintToModelCurveMapping = new ModelCurveArray();
                    FootPrintRoof   footprintRoof = activeDocument.Create.NewFootPrintRoof(toRoof.getProfile(), level, roofType, out footPrintToModelCurveMapping);
                }
                t.Commit();
            }
        }
Example #20
0
        public ElementId CreatePathRoof(XYZ[] pts, double[] angles, Level level, ElementId famId)
        {
            RoofType roofType = null;

            if (famId != null)
            {
                roofType = doc.GetElement(famId) as RoofType;
            }
            else
            {
                var roofTypeList = new FilteredElementCollector(doc).OfClass(typeof(RoofType));
                roofType = roofTypeList.FirstOrDefault(e =>
                                                       e.Name.Equals("Generic - 125mm") ||
                                                       e.Name.Equals("Generic Roof - 300mm")) as RoofType;
                if (roofType == null)
                {
                    roofType = roofTypeList.First() as RoofType;
                }
            }
            ModelCurveArray curveArray = new ModelCurveArray();
            FootPrintRoof   roof       = doc.Create.NewFootPrintRoof(ClosedPathCurveArray(pts, angles), level, roofType, out curveArray);

            return(roof.Id);
        }
    /// <summary>
    /// Add a roof over the rectangular profile of the walls we created earlier. 
    /// </summary>
    public void AddRoof(List<Wall> walls)
    {
      // Hard coding the roof type we will use. 
      // E.g., "Basic Roof: Generic - 400mm" 

      const string roofFamilyName = "Basic Roof";
      const string roofTypeName = Util.Constant.RoofTypeName; //  Util.Constant.RoofTypeName 
      const string roofFamilyAndTypeName = roofFamilyName + ": " + roofTypeName;

      // Find the roof type 

      RoofType roofType = (RoofType)ElementFiltering.FindFamilyType(_doc, typeof(RoofType), roofFamilyName, roofTypeName, null);

      if (roofType == null)
      {
        TaskDialog.Show(
          "Add roof",
          "Cannot find (" +
          roofFamilyAndTypeName +
          "). Maybe you use a different template? Try with DefaultMetric.rte.");
      }

      // Wall thickness to adjust the footprint of the walls 
      // to the outer most lines. 
      // Note: this may not be the best way, 
      // but we will live with this for this exercise. 

      //double wallThickness = walls[0].WallType.CompoundStructure.Layers.get_Item(0).Thickness; // 2011
      double wallThickness = walls[0].Width;

      double dt = wallThickness / 2.0;
      List<XYZ> dts = new List<XYZ>(5);
      dts.Add(new XYZ(-dt, -dt, 0.0));
      dts.Add(new XYZ(dt, -dt, 0.0));
      dts.Add(new XYZ(dt, dt, 0.0));
      dts.Add(new XYZ(-dt, dt, 0.0));
      dts.Add(dts[0]);

      // Set the profile from four walls 

      CurveArray footPrint = new CurveArray();
      for (int i = 0; i <= 3; i++)
      {
        LocationCurve locCurve = (LocationCurve)walls[i].Location;
        XYZ pt1 = locCurve.Curve.GetEndPoint(0) + dts[i];
        XYZ pt2 = locCurve.Curve.GetEndPoint(1) + dts[i + 1];
        Line line = Line.CreateBound(pt1, pt2);
        footPrint.Append(line);
      }

      // Get the level2 from the wall 

      ElementId idLevel2 = walls[0].get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId();
      //Level level2 = (Level)_doc.get_Element(idLevel2); // 2012
      Level level2 = (Level)_doc.GetElement(idLevel2); // since 2013

      // Footprint to model curve mapping 

      ModelCurveArray mapping = new ModelCurveArray();

      // Create a roof. 

      FootPrintRoof aRoof = _doc.Create.NewFootPrintRoof(
        footPrint, level2, roofType, out mapping);

      foreach (ModelCurve modelCurve in mapping)
      {
        aRoof.set_DefinesSlope(modelCurve, true);
        aRoof.set_SlopeAngle(modelCurve, 0.5);
      }
    }
        void SetModelCurvesColor(
            ModelCurveArray modelCurves,
            View view,
            Color color)
        {
            foreach( var curve in modelCurves
            .Cast<ModelCurve>() )
              {
            var overrides = view.GetElementOverrides(
              curve.Id );

            overrides.SetProjectionLineColor( color );

            view.SetElementOverrides( curve.Id, overrides );
              }
        }
Example #23
0
        public override Value Evaluate(FSharpList<Value> args)
        {
            var doc = dynRevitSettings.Doc;
            var refCurveList = ((Value.List)args[0]).Item.Select(
               x => ( ((Value.Container)x).Item is ModelCurve ?
                   ((ModelCurve)((Value.Container)x).Item)
                   : (ModelCurve)(
                                      doc.Document.GetElement(
                                             ((Reference) ((Value.Container)x).Item).ElementId)
                                                             )
                                 )
                   ).ToList();

            ModelCurveArray myModelCurves = new ModelCurveArray();

            //Plane thisPlane = null;
            //Line oneLine = null;

            List<ElementId> refIds = new List<ElementId>();
            XYZ loopStart = new XYZ();
            XYZ otherEnd = new XYZ();
            int index = 0;
            double tolerance = 0.000000001;
            foreach( var refCurve in refCurveList)
            {
                if (index == 0)
                {
                    loopStart = refCurve.GeometryCurve.Evaluate(0.0, true);
                    otherEnd = refCurve.GeometryCurve.Evaluate(1.0, true);
                }
                else //if (index > 0)
                {
                    XYZ startXYZ = refCurve.GeometryCurve.Evaluate(0.0, true);
                    XYZ endXYZ = refCurve.GeometryCurve.Evaluate(1.0, true);
                    if (index == 1)
                    {
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) > tolerance &&
                            (startXYZ.DistanceTo(loopStart) > tolerance || endXYZ.DistanceTo(loopStart) > tolerance))
                        {
                            XYZ temp = loopStart;
                            loopStart = otherEnd;
                            otherEnd = temp;
                        }
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) < tolerance)
                            otherEnd = startXYZ;
                        else if (startXYZ.DistanceTo(otherEnd) <tolerance && endXYZ.DistanceTo(otherEnd) >tolerance)
                            otherEnd = endXYZ;
                        else
                            throw new Exception("Gap between curves in chain of reference curves.");
                    }
                }
                /* not needed check
                if (refCurve.GeometryCurve is Line)
                {
                    Line thisLine = refCurve.GeometryCurve as Line;
                    if (thisPlane != null)
                    {
                        if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Direction)) > tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                        if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Origin - thisPlane.Origin)) > tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                    }
                    else if (oneLine == null)
                        oneLine = thisLine;
                    else
                    {
                        if (Math.Abs(oneLine.Direction.DotProduct(thisLine.Direction)) > 1.0 - tolerance)
                        {
                            double projAdjust = oneLine.Direction.DotProduct(oneLine.Origin - thisLine.Origin);
                            XYZ adjustedOrigin = thisLine.Origin + projAdjust * oneLine.Direction;
                            if (adjustedOrigin.DistanceTo(oneLine.Origin) > tolerance)
                                throw new Exception(" Planar Ref Curve Chain fails: not planar");
                        }
                        else
                        {
                            XYZ norm = oneLine.Direction.CrossProduct(thisLine.Direction);
                            norm = norm.Normalize();
                            thisPlane = new Plane(norm, oneLine.Origin);
                            if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Origin - thisPlane.Origin)) > tolerance)
                                throw new Exception(" Planar Ref Curve Chain fails: not planar");
                        }

                    }
                }
                else
                {
                    CurveLoop curveLoop = new CurveLoop();
                    curveLoop.Append(refCurve.GeometryCurve);
                    if (!curveLoop.HasPlane())
                        throw new Exception(" Planar Ref Curve Chain fails: curve is not planar.");
                    Plane curvePlane = curveLoop.GetPlane();
                    if (thisPlane == null && oneLine == null)
                        thisPlane = curveLoop.GetPlane();
                    else if (thisPlane != null)
                    {
                        if (Math.Abs(thisPlane.Normal.DotProduct(curvePlane.Normal)) < 1.0 - tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                        if (Math.Abs(thisPlane.Normal.DotProduct(curvePlane.Origin - thisPlane.Origin)) > tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                    }
                    else if (oneLine != null)
                    {
                        thisPlane = curvePlane;
                        if (Math.Abs(thisPlane.Normal.DotProduct(oneLine.Direction)) > tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                        if (Math.Abs(thisPlane.Normal.DotProduct(oneLine.Origin - thisPlane.Origin)) > tolerance)
                            throw new Exception(" Planar Ref Curve Chain fails: not planar");
                    }
                }
                */

                refIds.Add(refCurve.Id);
                myModelCurves.Append(refCurve);
                index++;
            }

            List<ElementId> removeIds = new List<ElementId>();
            foreach (ElementId oldId in this.Elements)
            {
                if (!refIds.Contains(oldId))
                {
                    removeIds.Add(oldId);
                }
            }

            foreach (ElementId removeId in removeIds)
            {
                    this.Elements.Remove(removeId);
            }
            foreach (ElementId newId in refIds)
            {
                if (!this.Elements.Contains(newId))
                    this.Elements.Add(newId);
            }
            //if (!curveLoop.HasPlane())
            //    throw new Exception(" Planar Ref Curve Chain fails: not planar");
            return Value.NewContainer(myModelCurves);
        }
Example #24
0
        /// <summary>
        /// Improved implementation by Alexander Ignatovich
        /// supporting curved wall with curved window,
        /// second attempt, published April 10, 2015:
        /// </summary>
        public Result Execute3(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;
            View          view  = doc.ActiveView;

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

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

            Reference r = uidoc.Selection.PickObject(
                ObjectType.Element, "Select a wall");

            Element e = uidoc.Document.GetElement(r);

            Creator creator = new Creator(doc);

            Wall wall = e as Wall;

            if (wall == null)
            {
                return(Result.Cancelled);
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Wall Profile");

                // Get the external wall face for the profile
                // a little bit simpler than in the last realization

                Reference sideFaceReference
                    = HostObjectUtils.GetSideFaces(
                          wall, ShellLayerType.Exterior)
                      .First();

                Face face = wall.GetGeometryObjectFromReference(
                    sideFaceReference) as Face;

                // The normal of the wall external face.

                XYZ normal = wall.Orientation;

                // Offset curve copies for visibility.

                Transform offset = Transform.CreateTranslation(
                    5 * normal);

                // If the curve loop direction is counter-
                // clockwise, change its color to RED.

                Color colorRed = new Color(255, 0, 0);

                // Get edge loops as curve loops.

                IList <CurveLoop> curveLoops
                    = face.GetEdgesAsCurveLoops();

                foreach (var curveLoop in curveLoops)
                {
                    CurveArray curves = creapp.NewCurveArray();

                    foreach (Curve curve in curveLoop)
                    {
                        curves.Append(curve.CreateTransformed(
                                          offset));
                    }

                    var isCounterClockwize = curveLoop
                                             .IsCounterclockwise(normal);

                    // Create model lines for an curve loop if it is made

                    if (((LocationCurve)wall.Location).Curve
                        is Line)
                    {
                        //Plane plane = creapp.NewPlane( curves ); // 2016
                        Plane plane = CurveLoop.CreateViaOffset(
                            curveLoop, 5 * normal.GetLength(),
                            normal.Normalize()).GetPlane(); // 2017

                        SketchPlane sketchPlane
                            = SketchPlane.Create(doc, plane);

                        ModelCurveArray curveElements = credoc
                                                        .NewModelCurveArray(curves, sketchPlane);

                        if (isCounterClockwize)
                        {
                            SetModelCurvesColor(curveElements,
                                                view, colorRed);
                        }
                    }
                    else
                    {
                        foreach (var curve in curves.Cast <Curve>())
                        {
                            var curveElements = creator.CreateModelCurves(curve);
                            if (isCounterClockwize)
                            {
                                SetModelCurvesColor(curveElements, view, colorRed);
                            }
                        }
                    }
                }
                tx.Commit();
            }
            return(Result.Succeeded);
        }
Example #25
0
        List<RevitParameter> ILyrebirdService.GetParameters(RevitObject revitFamily, string typeName)
        {
            lock (_locker)
            {
                TaskContainer.Instance.EnqueueTask(uiApp =>
                {
                    var doc = uiApp.ActiveUIDocument.Document;
                    parameters = new List<RevitParameter>();
                    if (revitFamily.CategoryId == -2000011)
                    {
                        // do stuff for walls
                        FilteredElementCollector wallCollector = new FilteredElementCollector(uiApp.ActiveUIDocument.Document);
                        wallCollector.OfClass(typeof(WallType));
                        wallCollector.OfCategory(BuiltInCategory.OST_Walls);
                        foreach (WallType wt in wallCollector)
                        {
                            if (wt.Name == typeName)
                            {
                                // Get the type parameters
                                List<Parameter> typeParams = new List<Parameter>();
                                foreach (Parameter p in wt.Parameters)
                                {
                                    if(!p.IsReadOnly)
                                        typeParams.Add(p);
                                }

                                // Get the instance parameters
                                List<Parameter> instParameters = new List<Parameter>();
                                using (Transaction t = new Transaction(doc, "temp family"))
                                {
                                    t.Start();
                                    Wall wall = null;
                                    try
                                    {
                                        Curve c = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                        FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                        Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                        if (l != null) wall = Wall.Create(doc, c, l.Id, false);
                                    }
                                    catch (Exception exception)
                                    {
                                        // Failed to create the wall, no instance parameters will be found
                                        Debug.WriteLine(exception.Message);
                                    }

                                    if (wall != null)
                                    {
                                        foreach (Parameter p in wall.Parameters)
                                        {
                                            //if(!p.IsReadOnly)
                                                instParameters.Add(p);
                                        }
                                    }
                                    t.RollBack();
                                }
                                typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                instParameters.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                foreach (Parameter p in typeParams)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = true
                                    };
                                    parameters.Add(rp);
                                }
                                foreach (Parameter p in instParameters)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = false
                                    };

                                    parameters.Add(rp);
                                }
                                break;
                            }
                        }
                    }
                    else if (revitFamily.CategoryId == -2000032)
                    {
                        // get parameters for floors
                        FilteredElementCollector floorCollector = new FilteredElementCollector(doc);
                        floorCollector.OfClass(typeof(FloorType));
                        floorCollector.OfCategory(BuiltInCategory.OST_Floors);
                        foreach (FloorType ft in floorCollector)
                        {
                            if (ft.Name == typeName)
                            {
                                // Get the type parameters
                                List<Parameter> typeParams = new List<Parameter>();
                                foreach (Parameter p in ft.Parameters)
                                {
                                    if(!p.IsReadOnly)
                                        typeParams.Add(p);
                                }

                                // Get the instance parameters
                                List<Parameter> instParameters = new List<Parameter>();
                                using (Transaction t = new Transaction(doc, "temp family"))
                                {
                                    t.Start();
                                    Floor floor = null;
                                    try
                                    {
                                        Curve c1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                        Curve c2 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(1, 1, 0));
                                        Curve c3 = Line.CreateBound(new XYZ(1, 1, 0), new XYZ(0, 1, 0));
                                        Curve c4 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(0, 0, 0));
                                        CurveArray profile = new CurveArray();
                                        profile.Append(c1);
                                        profile.Append(c2);
                                        profile.Append(c3);
                                        profile.Append(c4);
                                        floor = doc.Create.NewFloor(profile, false);
                                    }

                                    catch (Exception ex)
                                    {
                                        // Failed to create the wall, no instance parameters will be found
                                        Debug.WriteLine(ex.Message);
                                    }
                                    if (floor != null)
                                    {
                                        foreach (Parameter p in floor.Parameters)
                                        {
                                            if (!p.IsReadOnly)
                                                instParameters.Add(p);
                                        }
                                    }
                                    t.RollBack();
                                }
                                typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                instParameters.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                foreach (Parameter p in typeParams)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = true
                                    };

                                    parameters.Add(rp);
                                }
                                foreach (Parameter p in instParameters)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = false
                                    };

                                    parameters.Add(rp);
                                }
                                break;
                            }
                        }
                    }
                    else if (revitFamily.CategoryId == -2000035)
                    {
                        // get parameters for a roof
                        FilteredElementCollector roofCollector = new FilteredElementCollector(doc);
                        roofCollector.OfClass(typeof(RoofType));
                        roofCollector.OfCategory(BuiltInCategory.OST_Roofs);
                        foreach (RoofType rt in roofCollector)
                        {
                            if (rt.Name == typeName)
                            {
                                // Get the type parameters
                                List<Parameter> typeParams = new List<Parameter>();
                                foreach (Parameter p in rt.Parameters)
                                {
                                    if (!p.IsReadOnly)
                                        typeParams.Add(p);
                                }

                                // Get the instance parameters
                                List<Parameter> instParameters = new List<Parameter>();
                                using (Transaction t = new Transaction(doc, "temp family"))
                                {
                                    t.Start();
                                    FootPrintRoof roof = null;
                                    try
                                    {
                                        Curve c1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                        Curve c2 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(1, 1, 0));
                                        Curve c3 = Line.CreateBound(new XYZ(1, 1, 0), new XYZ(0, 1, 0));
                                        Curve c4 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(0, 0, 0));
                                        CurveArray profile = new CurveArray();
                                        profile.Append(c1);
                                        profile.Append(c2);
                                        profile.Append(c3);
                                        profile.Append(c4);
                                        FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                        Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                        ModelCurveArray curveArrayMapping = new ModelCurveArray();
                                        roof = doc.Create.NewFootPrintRoof(profile, l, rt, out curveArrayMapping);
                                    }
                                    catch (Exception ex)
                                    {
                                        // Failed to create the wall, no instance parameters will be found
                                        Debug.WriteLine(ex.Message);
                                    }
                                    if (roof != null)
                                    {
                                        foreach (Parameter p in roof.Parameters)
                                        {
                                            if (!p.IsReadOnly)
                                                instParameters.Add(p);
                                        }
                                    }
                                    t.RollBack();
                                }
                                typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                instParameters.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                foreach (Parameter p in typeParams)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = true
                                    };

                                    parameters.Add(rp);
                                }
                                foreach (Parameter p in instParameters)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = false
                                    };

                                    parameters.Add(rp);
                                }
                                break;
                            }
                        }
                    }
                    else if (revitFamily.CategoryId == -2000240)
                    {
                        // Level Families
                        FilteredElementCollector levelCollector = new FilteredElementCollector(doc);
                        levelCollector.OfClass(typeof(LevelType));
                        levelCollector.OfCategory(BuiltInCategory.OST_Levels);
                        foreach (LevelType lt in levelCollector)
                        {
                            if (lt.Name == typeName)
                            {
                                // Get the type parameters
                                List<Parameter> typeParams = new List<Parameter>();
                                foreach (Parameter p in lt.Parameters)
                                {
                                    if (!p.IsReadOnly)
                                        typeParams.Add(p);
                                }

                                // Get the instance parameters
                                List<Parameter> instParameters = new List<Parameter>();
                                using (Transaction t = new Transaction(doc, "temp level"))
                                {
                                    t.Start();
                                    Level lvl = null;
                                    try
                                    {
                                        lvl = doc.Create.NewLevel(-1000.22);
                                    }
                                    catch (Exception ex)
                                    {
                                        // Failed to create the wall, no instance parameters will be found
                                        Debug.WriteLine(ex.Message);
                                    }
                                    if (lvl != null)
                                    {
                                        foreach (Parameter p in lvl.Parameters)
                                        {
                                            if (!p.IsReadOnly)
                                                instParameters.Add(p);
                                        }
                                    }
                                    t.RollBack();
                                }
                                typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                instParameters.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                foreach (Parameter p in typeParams)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = true
                                    };

                                    parameters.Add(rp);
                                }
                                foreach (Parameter p in instParameters)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = false
                                    };

                                    parameters.Add(rp);
                                }
                                break;
                            }
                        }
                    }
                    else if (revitFamily.CategoryId == -2000220)
                    {
                        // Grid Families
                        FilteredElementCollector gridCollector = new FilteredElementCollector(doc);
                        gridCollector.OfClass(typeof(GridType));
                        gridCollector.OfCategory(BuiltInCategory.OST_Grids);
                        foreach (GridType gt in gridCollector)
                        {
                            if (gt.Name == typeName)
                            {
                                // Get the type parameters
                                List<Parameter> typeParams = new List<Parameter>();
                                foreach (Parameter p in gt.Parameters)
                                {
                                    if (!p.IsReadOnly)
                                        typeParams.Add(p);
                                }

                                // Get the instance parameters
                                List<Parameter> instParameters = new List<Parameter>();
                                using (Transaction t = new Transaction(doc, "temp grid"))
                                {
                                    t.Start();
                                    Grid grid = null;
                                    try
                                    {
                                        Line ln = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 1, 0));
                                        grid = doc.Create.NewGrid(ln);
                                    }
                                    catch (Exception ex)
                                    {
                                        // Failed to create the wall, no instance parameters will be found
                                        Debug.WriteLine(ex.Message);
                                    }
                                    if (grid != null)
                                    {
                                        foreach (Parameter p in grid.Parameters)
                                        {
                                            if (!p.IsReadOnly)
                                                instParameters.Add(p);
                                        }
                                    }
                                    t.RollBack();
                                }
                                typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                instParameters.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                foreach (Parameter p in typeParams)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = true
                                    };

                                    parameters.Add(rp);
                                }
                                foreach (Parameter p in instParameters)
                                {
                                    RevitParameter rp = new RevitParameter
                                    {
                                        ParameterName = p.Definition.Name,
                                        StorageType = p.StorageType.ToString(),
                                        IsType = false
                                    };

                                    parameters.Add(rp);
                                }
                                break;
                            }
                        }
                    }
                    else if (revitFamily.CategoryId == -2000051)
                    {
                        // leave parameters empty
                    }
                    else
                    {
                        // Regular family.  Proceed to get all parameters
                        FilteredElementCollector familyCollector = new FilteredElementCollector(doc);
                        familyCollector.OfClass(typeof(Family));
                        foreach (Family f in familyCollector)
                        {
                            if (f.Name == revitFamily.FamilyName)
                            {
                                ISet<ElementId> fsIds = f.GetFamilySymbolIds();

                                foreach (ElementId fsid in fsIds)
                                {
                                    FamilySymbol fs = doc.GetElement(fsid) as FamilySymbol;
                                    if (fs.Name == typeName)
                                    {
                                        List<Parameter> typeParams = new List<Parameter>();
                                        foreach (Parameter p in fs.Parameters)
                                        {
                                            if (!p.IsReadOnly)
                                                typeParams.Add(p);
                                        }
                                        List<Parameter> instanceParams = new List<Parameter>();
                                        // temporary create an instance of the family to get instance parameters
                                        using (Transaction t = new Transaction(doc, "temp family"))
                                        {
                                            t.Start();
                                            FamilyInstance fi = null;
                                            // Get the hosting type

                                            int hostType = f.get_Parameter(BuiltInParameter.FAMILY_HOSTING_BEHAVIOR).AsInteger();
                                            if (hostType == 0)
                                            {
                                                // Typical
                                            }
                                            else if (hostType == 1)
                                            {
                                                // Wall hosted
                                                // Temporary wall
                                                Wall wall = null;
                                                Curve c = Line.CreateBound(new XYZ(-20, 0, 0), new XYZ(20, 0, 0));
                                                FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                                Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                                try
                                                {
                                                    if (l != null) wall = Wall.Create(doc, c, l.Id, false);
                                                }
                                                catch (Exception ex)
                                                {
                                                    // Failed to create the wall, no instance parameters will be found
                                                    Debug.WriteLine(ex.Message);
                                                }
                                                if (wall != null)
                                                {
                                                    fi = doc.Create.NewFamilyInstance(new XYZ(0, 0, 0), fs, wall as Element, l, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                                else
                                                {
                                                    // regular creation.  Some parameters will be missing
                                                    fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                            }
                                            else if (hostType == 2)
                                            {
                                                // Floor Hosted
                                                // temporary floor
                                                Floor floor = null;
                                                FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                                Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                                try
                                                {
                                                    Curve c1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                                    Curve c2 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(1, 1, 0));
                                                    Curve c3 = Line.CreateBound(new XYZ(1, 1, 0), new XYZ(0, 1, 0));
                                                    Curve c4 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(0, 0, 0));
                                                    CurveArray profile = new CurveArray();
                                                    profile.Append(c1);
                                                    profile.Append(c2);
                                                    profile.Append(c3);
                                                    profile.Append(c4);
                                                    floor = doc.Create.NewFloor(profile, false);
                                                }
                                                catch (Exception ex)
                                                {
                                                    // Failed to create the floor, no instance parameters will be found
                                                    Debug.WriteLine(ex.Message);
                                                }
                                                if (floor != null)
                                                {
                                                    fi = doc.Create.NewFamilyInstance(new XYZ(0, 0, 0), fs, floor as Element, l, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                                else
                                                {
                                                    // regular creation.  Some parameters will be missing
                                                    fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                            }
                                            else if (hostType == 3)
                                            {
                                                // Ceiling Hosted (might be difficult)
                                                // Try to find a ceiling
                                                FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                                Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                                FilteredElementCollector ceilingCollector = new FilteredElementCollector(doc);
                                                Ceiling ceiling = ceilingCollector.OfClass(typeof(Ceiling)).ToElements().OfType<Ceiling>().FirstOrDefault();
                                                if (ceiling != null)
                                                {
                                                    // Find a point on the ceiling
                                                    Options opt = new Options();
                                                    opt.ComputeReferences = true;
                                                    GeometryElement ge = ceiling.get_Geometry(opt);
                                                    List<List<XYZ>> verticePoints = new List<List<XYZ>>();
                                                    foreach (GeometryObject go in ge)
                                                    {
                                                        Solid solid = go as Solid;
                                                        if (null == solid || 0 == solid.Faces.Size)
                                                        {
                                                            continue;
                                                        }

                                                        PlanarFace planarFace = null;
                                                        double faceArea = 0;
                                                        foreach (Face face in solid.Faces)
                                                        {

                                                            PlanarFace pf = null;
                                                            try
                                                            {
                                                                pf = face as PlanarFace;
                                                            }
                                                            catch
                                                            {
                                                            }
                                                            if (pf != null)
                                                            {
                                                                if (pf.Area > faceArea)
                                                                {
                                                                    planarFace = pf;
                                                                }
                                                            }
                                                        }
                                                        if (planarFace != null)
                                                        {
                                                            Mesh mesh = planarFace.Triangulate();
                                                            int triCnt = mesh.NumTriangles;
                                                            MeshTriangle bigTriangle = null;
                                                            for (int tri = 0; tri < triCnt; tri++)
                                                            {
                                                                if (bigTriangle == null)
                                                                {

                                                                    bigTriangle = mesh.get_Triangle(tri);
                                                                }
                                                                else
                                                                {
                                                                    MeshTriangle mt = mesh.get_Triangle(tri);
                                                                    double area = Math.Abs(((mt.get_Vertex(0).X * (mt.get_Vertex(1).Y - mt.get_Vertex(2).Y)) + (mt.get_Vertex(1).X * (mt.get_Vertex(2).Y - mt.get_Vertex(0).Y)) + (mt.get_Vertex(2).X * (mt.get_Vertex(0).Y - mt.get_Vertex(1).Y))) / 2);
                                                                    double bigTriArea = Math.Abs(((bigTriangle.get_Vertex(0).X * (bigTriangle.get_Vertex(1).Y - bigTriangle.get_Vertex(2).Y)) + (bigTriangle.get_Vertex(1).X * (bigTriangle.get_Vertex(2).Y - bigTriangle.get_Vertex(0).Y)) + (bigTriangle.get_Vertex(2).X * (bigTriangle.get_Vertex(0).Y - bigTriangle.get_Vertex(1).Y))) / 2);
                                                                    if (area > bigTriArea)
                                                                    {
                                                                        bigTriangle = mt;
                                                                    }
                                                                }
                                                            }
                                                            if (bigTriangle != null)
                                                            {
                                                                double test = Math.Abs(((bigTriangle.get_Vertex(0).X * (bigTriangle.get_Vertex(1).Y - bigTriangle.get_Vertex(2).Y)) + (bigTriangle.get_Vertex(1).X * (bigTriangle.get_Vertex(2).Y - bigTriangle.get_Vertex(0).Y)) + (bigTriangle.get_Vertex(2).X * (bigTriangle.get_Vertex(0).Y - bigTriangle.get_Vertex(1).Y))) / 2);
                                                            }
                                                            try
                                                            {
                                                                List<XYZ> ptList = new List<XYZ>();
                                                                ptList.Add(bigTriangle.get_Vertex(0));
                                                                ptList.Add(bigTriangle.get_Vertex(1));
                                                                ptList.Add(bigTriangle.get_Vertex(2));
                                                                verticePoints.Add(ptList);
                                                            }
                                                            catch
                                                            {
                                                            }
                                                            break;
                                                        }
                                                    }

                                                    if (verticePoints.Count > 0)
                                                    {
                                                        List<XYZ> vertices = verticePoints[0];
                                                        XYZ midXYZ = vertices[1] + (0.5 * (vertices[2] - vertices[1]));
                                                        XYZ centerPt = vertices[0] + (0.666667 * (midXYZ - vertices[0]));

                                                        if (ceiling != null)
                                                        {
                                                            fi = doc.Create.NewFamilyInstance(centerPt, fs, ceiling as Element, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                        }
                                                        else
                                                        {
                                                            // regular creation.  Some parameters will be missing
                                                            fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                        }

                                                    }
                                                }
                                            }
                                            else if (hostType == 4)
                                            {
                                                // Roof Hosted
                                                // Temporary roof
                                                FootPrintRoof roof = null;
                                                FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                                Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                                FilteredElementCollector roofTypeCollector = new FilteredElementCollector(doc);
                                                RoofType rt = roofTypeCollector.OfClass(typeof(RoofType)).ToElements().OfType<RoofType>().FirstOrDefault();
                                                try
                                                {
                                                    Curve c1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                                    Curve c2 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(1, 1, 0));
                                                    Curve c3 = Line.CreateBound(new XYZ(1, 1, 0), new XYZ(0, 1, 0));
                                                    Curve c4 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(0, 0, 0));
                                                    CurveArray profile = new CurveArray();
                                                    profile.Append(c1);
                                                    profile.Append(c2);
                                                    profile.Append(c3);
                                                    profile.Append(c4);
                                                    ModelCurveArray roofProfile = new ModelCurveArray();
                                                    roof = doc.Create.NewFootPrintRoof(profile, l, rt, out roofProfile);
                                                }
                                                catch (Exception ex)
                                                {
                                                    // Failed to create the roof, no instance parameters will be found
                                                    Debug.WriteLine(ex.Message);
                                                }

                                                if (roof != null)
                                                {
                                                    fi = doc.Create.NewFamilyInstance(new XYZ(0, 0, 0), fs, roof as Element, l, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                                else
                                                {
                                                    // regular creation.  Some parameters will be missing
                                                    fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                            }
                                            else if (hostType == 5)
                                            {
                                                // temporary floor
                                                Floor floor = null;
                                                FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
                                                Level l = lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>().FirstOrDefault();
                                                try
                                                {
                                                    Curve c1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0));
                                                    Curve c2 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(1, 1, 0));
                                                    Curve c3 = Line.CreateBound(new XYZ(1, 1, 0), new XYZ(0, 1, 0));
                                                    Curve c4 = Line.CreateBound(new XYZ(0, 1, 0), new XYZ(0, 0, 0));
                                                    CurveArray profile = new CurveArray();
                                                    profile.Append(c1);
                                                    profile.Append(c2);
                                                    profile.Append(c3);
                                                    profile.Append(c4);
                                                    floor = doc.Create.NewFloor(profile, false);
                                                }
                                                catch (Exception ex)
                                                {
                                                    // Failed to create the floor, no instance parameters will be found
                                                    Debug.WriteLine(ex.Message);
                                                }

                                                // Find a face on the floor to host to.
                                                Face face = FindFace(XYZ.Zero, XYZ.BasisZ, doc);
                                                if (face != null)
                                                {
                                                    fi = doc.Create.NewFamilyInstance(face, XYZ.Zero, new XYZ(0, -1, 0), fs);
                                                }
                                                else
                                                {
                                                    // regular creation.  Some parameters will be missing
                                                    fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                }
                                            }
                                            // Create a typical family instance
                                            try
                                            {
                                                fi = doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                            }
                                            catch (Exception ex)
                                            {
                                                Debug.WriteLine(ex.Message);
                                            }
                                            // TODO: Try creating other family instances like walls, sketch based, ... and getting the instance params
                                            if (fi != null)
                                            {
                                                foreach (Parameter p in fi.Parameters)
                                                {
                                                    if (!p.IsReadOnly)
                                                        instanceParams.Add(p);
                                                }
                                            }

                                            t.RollBack();
                                        }

                                        typeParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                        instanceParams.Sort((x, y) => String.CompareOrdinal(x.Definition.Name, y.Definition.Name));
                                        foreach (Parameter p in typeParams)
                                        {
                                            RevitParameter rp = new RevitParameter
                                            {
                                                ParameterName = p.Definition.Name,
                                                StorageType = p.StorageType.ToString(),
                                                IsType = true
                                            };

                                            parameters.Add(rp);
                                        }
                                        foreach (Parameter p in instanceParams)
                                        {
                                            RevitParameter rp = new RevitParameter
                                            {
                                                ParameterName = p.Definition.Name,
                                                StorageType = p.StorageType.ToString(),
                                                IsType = false
                                            };

                                            parameters.Add(rp);
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                });
            }
            return parameters;
        }
Example #26
0
        /// <summary>
        /// Alternative implementation published January 23, 2015:
        /// http://thebuildingcoder.typepad.com/blog/2015/01/getting-the-wall-elevation-profile.html
        /// </summary>
        public Result Execute2(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;
            View          view  = doc.ActiveView;

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

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

            Reference r = uidoc.Selection.PickObject(
                ObjectType.Element, "Select a wall");

            Element e = uidoc.Document.GetElement(r);

            Wall wall = e as Wall;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Wall Profile");

                // Get the external wall face for the profile

                IList <Reference> sideFaces
                    = HostObjectUtils.GetSideFaces(wall,
                                                   ShellLayerType.Exterior);

                Element e2 = doc.GetElement(sideFaces[0]);

                Face face = e2.GetGeometryObjectFromReference(
                    sideFaces[0]) as Face;

                // The normal of the wall external face.

                XYZ normal = face.ComputeNormal(new UV(0, 0));

                // Offset curve copies for visibility.

                Transform offset = Transform.CreateTranslation(
                    5 * normal);

                // If the curve loop direction is counter-
                // clockwise, change its color to RED.

                Color colorRed = new Color(255, 0, 0);

                // Get edge loops as curve loops.

                IList <CurveLoop> curveLoops
                    = face.GetEdgesAsCurveLoops();

                // ExporterIFCUtils class can also be used for
                // non-IFC purposes. The SortCurveLoops method
                // sorts curve loops (edge loops) so that the
                // outer loops come first.

                IList <IList <CurveLoop> > curveLoopLoop
                    = ExporterIFCUtils.SortCurveLoops(
                          curveLoops);

                foreach (IList <CurveLoop> curveLoops2
                         in curveLoopLoop)
                {
                    foreach (CurveLoop curveLoop2 in curveLoops2)
                    {
                        // Check if curve loop is counter-clockwise.

                        bool isCCW = curveLoop2.IsCounterclockwise(
                            normal);

                        CurveArray curves = creapp.NewCurveArray();

                        foreach (Curve curve in curveLoop2)
                        {
                            curves.Append(curve.CreateTransformed(offset));
                        }

                        // Create model lines for an curve loop.

                        //Plane plane = creapp.NewPlane( curves ); // 2016

                        Plane plane = curveLoop2.GetPlane(); // 2017

                        SketchPlane sketchPlane
                            = SketchPlane.Create(doc, plane);

                        ModelCurveArray curveElements
                            = credoc.NewModelCurveArray(curves,
                                                        sketchPlane);

                        if (isCCW)
                        {
                            foreach (ModelCurve mcurve in curveElements)
                            {
                                OverrideGraphicSettings overrides
                                    = view.GetElementOverrides(
                                          mcurve.Id);

                                overrides.SetProjectionLineColor(
                                    colorRed);

                                view.SetElementOverrides(
                                    mcurve.Id, overrides);
                            }
                        }
                    }
                }
                tx.Commit();
            }
            return(Result.Succeeded);
        }
Example #27
0
        private void CreateObjects(List<RevitObject> revitObjects, Document doc, Guid uniqueId, int runId, string nickName)
        {
            // Create new Revit objects.
            //List<LyrebirdId> newUniqueIds = new List<LyrebirdId>();

            // Get the levels from the project
            FilteredElementCollector lvlCollector = new FilteredElementCollector(doc);
            lvlCollector.OfClass(typeof(Level)).ToElements().OfType<Level>();

            // Determine what kind of object we're creating.
            RevitObject ro = revitObjects[0];

            #region Normal Origin based Family Instance
            if (ro.Origin != null)
            {
                // Find the FamilySymbol
                FamilySymbol symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);
                
                LevelType lt = FindLevelType(ro.TypeName, doc);

                if (symbol != null || lt != null)
                {
                    // Get the hosting ID from the family.
                    Family fam = null;
                    Parameter hostParam = null;
                    int hostBehavior = 0;

                    try
                    {
                        fam = symbol.Family;
                        hostParam = fam.get_Parameter(BuiltInParameter.FAMILY_HOSTING_BEHAVIOR);
                        hostBehavior = hostParam.AsInteger();
                    }
                    catch{}
                    
                    using (Transaction t = new Transaction(doc, "Lyrebird Create Objects"))
                    {
                        t.Start();
                        try
                        {
                            // Create the Schema for the instances to store the GH Component InstanceGUID and the path
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");
                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");
                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }
                            FamilyInstance fi = null;
                            XYZ origin = XYZ.Zero;
                            if (lt != null)
                            {
                                // Create a level for the object.
                                foreach (RevitObject obj in revitObjects)
                                {
                                    try
                                    {
                                        Level lvl = doc.Create.NewLevel(UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT));
                                        lvl.LevelType = lt;
                                        
                                        // Set the parameters.
                                        SetParameters(lvl, obj.Parameters, doc);

                                        // Assign the GH InstanceGuid
                                        AssignGuid(lvl, uniqueId, instanceSchema, runId, nickName);
                                    }
                                    catch { }
                                }
                            }
                            else if (hostBehavior == 0)
                            {
                                int x = 0;
                                foreach (RevitObject obj in revitObjects)
                                {
                                    try
                                    {
                                        List<LyrebirdPoint> originPts = new List<LyrebirdPoint>();
                                        Level lvl = GetLevel(originPts, doc);
                                        origin = new XYZ(UnitUtils.ConvertToInternalUnits(obj.Origin.X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT));
                                        if (symbol.Category.Id.IntegerValue == -2001330)
                                        {
                                            if (lvl != null)
                                            {
                                                // Structural Column
                                                fi = doc.Create.NewFamilyInstance(origin, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.Column);
                                                fi.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM).Set(origin.Z - lvl.Elevation);
                                                double topElev = ((Level)doc.GetElement(fi.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).AsElementId())).Elevation;
                                                if (lvl.Elevation + (origin.Z - lvl.Elevation) > topElev)
                                                {
                                                    fi.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM).Set((lvl.Elevation + (origin.Z - lvl.Elevation)) - topElev + 10.0);
                                                }
                                            }
                                            else
                                            {
                                                TaskDialog.Show("error", "Null level");
                                            }
                                        }
                                        else
                                        {
                                            // All Else
                                            fi = doc.Create.NewFamilyInstance(origin, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        TaskDialog.Show("Error", ex.Message);
                                    }

                                    // Rotate
                                    if (obj.Orientation != null)
                                    {
                                        if (Math.Round(Math.Abs(obj.Orientation.Z - 0), 10) < double.Epsilon)
                                        {
                                            Line axis = Line.CreateBound(origin, origin + XYZ.BasisZ);
                                            XYZ normalVector = new XYZ(0, -1, 0);
                                            XYZ orient = new XYZ(obj.Orientation.X, obj.Orientation.Y, obj.Orientation.Z);
                                            double angle = 0;
                                            if (orient.X < 0 && orient.Y < 0)
                                            {
                                                angle = (2 * Math.PI) - normalVector.AngleTo(orient);
                                            }
                                            else if (orient.X < 0)
                                            {
                                                angle = (Math.PI - normalVector.AngleTo(orient)) + Math.PI;
                                            }
                                            else if (orient.Y == 0)
                                            {
                                                angle = 1.5 * Math.PI;
                                            }
                                            else
                                            {
                                                angle = normalVector.AngleTo(orient);
                                            }
                                            ElementTransformUtils.RotateElement(doc, fi.Id, axis, angle);
                                        }
                                    }

                                    // Assign the parameters
                                    SetParameters(fi, obj.Parameters, doc);

                                    // Assign the GH InstanceGuid
                                    AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);

                                    x++;
                                }
                            }
                            else
                            {
                                foreach (RevitObject obj in revitObjects)
                                {
                                    origin = new XYZ(UnitUtils.ConvertToInternalUnits(obj.Origin.X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT));
                                    
                                    // Find the level
                                    List<LyrebirdPoint> lbPoints = new List<LyrebirdPoint> { obj.Origin };
                                    Level lvl = GetLevel(lbPoints, doc);
                                    
                                    // Get the host
                                    if (hostBehavior == 5)
                                    {
                                        // Face based family.  Find the face and create the element
                                        XYZ normVector = new XYZ(obj.Orientation.X, obj.Orientation.Y, obj.Orientation.Z);
                                        XYZ faceVector;
                                        if (obj.FaceOrientation != null)
                                        {
                                            faceVector = new XYZ(obj.FaceOrientation.X, obj.FaceOrientation.Y, obj.FaceOrientation.Z);
                                        }
                                        else
                                        {
                                            faceVector = XYZ.BasisZ;
                                        }
                                        Face face = FindFace(origin, normVector, doc);
                                        if (face != null)
                                        {
                                            fi = doc.Create.NewFamilyInstance(face, origin, faceVector, symbol);
                                        }
                                    }
                                    else
                                    {
                                        // typical hosted family.  Can be wall, floor, roof or ceiling.
                                        ElementId host = FindHost(origin, hostBehavior, doc);
                                        
                                        if (host != null)
                                        {
                                            fi = doc.Create.NewFamilyInstance(origin, symbol, doc.GetElement(host), lvl, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                        }
                                    }
                                    // Assign the parameters
                                    SetParameters(fi, obj.Parameters, doc);

                                    // Assign the GH InstanceGuid
                                    AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                }
                                // delete the host finder
                                ElementId hostFinderFamily = hostFinder.Symbol.Family.Id;
                                doc.Delete(hostFinder.Id);
                                doc.Delete(hostFinderFamily);
                                hostFinder = null;
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Error", ex.Message);
                        }
                        t.Commit();
                    }
                }
                else
                {
                    TaskDialog.Show("Error", "Could not find the desired family type");
                }

            }
            #endregion

            #region Adaptive Components
            else if (ro.AdaptivePoints != null && ro.AdaptivePoints.Count > 0)
            {
                // Find the FamilySymbol
                FamilySymbol symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);
                if (symbol != null)
                {
                    using (Transaction t = new Transaction(doc, "Lyrebird Create Objects"))
                    {
                        t.Start();
                        try
                        {
                            // Create the Schema for the instances to store the GH Component InstanceGUID and the path
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");

                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");

                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }
                            try
                            {
                                foreach (RevitObject obj in revitObjects)
                                {
                                    FamilyInstance fi = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, symbol);
                                    IList<ElementId> placePointIds = new List<ElementId>();
                                    placePointIds = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(fi);

                                    for (int ptNum = 0; ptNum < obj.AdaptivePoints.Count; ptNum++)
                                    {
                                        try
                                        {
                                            ReferencePoint rp = doc.GetElement(placePointIds[ptNum]) as ReferencePoint;
                                            XYZ pt = new XYZ(UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].Z, lengthDUT));
                                            if (rp != null)
                                            {
                                                XYZ vector = pt.Subtract(rp.Position);
                                                ElementTransformUtils.MoveElement(doc, rp.Id, vector);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine(ex.Message);
                                        }
                                    }

                                    // Assign the parameters
                                    SetParameters(fi, obj.Parameters, doc);

                                    // Assign the GH InstanceGuid
                                    AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                }

                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                        t.Commit();
                    }
                }
            }
            #endregion

            #region Curve based
            else if (ro.Curves != null && ro.Curves.Count > 0)
            {

                // Find the FamilySymbol
                FamilySymbol symbol = null;
                WallType wallType = null;
                FloorType floorType = null;
                RoofType roofType = null;
                GridType gridType = null;
                bool typeFound = false;

                FilteredElementCollector famCollector = new FilteredElementCollector(doc);

                if (ro.CategoryId == -2000011)
                {
                    famCollector.OfClass(typeof(WallType));
                    foreach (WallType wt in famCollector)
                    {
                        if (wt.Name == ro.TypeName)
                        {
                            wallType = wt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000032)
                {
                    famCollector.OfClass(typeof(FloorType));
                    foreach (FloorType ft in famCollector)
                    {
                        if (ft.Name == ro.TypeName)
                        {
                            floorType = ft;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000035)
                {
                    famCollector.OfClass(typeof(RoofType));
                    foreach (RoofType rt in famCollector)
                    {
                        if (rt.Name == ro.TypeName)
                        {
                            roofType = rt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000220)
                {
                    famCollector.OfClass(typeof(GridType));
                    foreach (GridType gt in famCollector)
                    {
                        if (gt.Name == ro.TypeName)
                        {
                            gridType = gt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else
                {
                    symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);
                    if (symbol != null)
                        typeFound = true;
                }



                if (typeFound)
                {
                    using (Transaction t = new Transaction(doc, "Lyrebird Create Objects"))
                    {

                        t.Start();
                        try
                        {
                            // Create the Schema for the instances to store the GH Component InstanceGUID and the path
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");

                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");

                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }
                            FamilyInstance fi = null;
                            try
                            {

                                foreach (RevitObject obj in revitObjects)
                                {

                                    #region single line based family
                                    if (obj.Curves.Count == 1 && obj.Curves[0].CurveType != "Circle")
                                    {

                                        LyrebirdCurve lbc = obj.Curves[0];
                                        List<LyrebirdPoint> curvePoints = lbc.ControlPoints.OrderBy(p => p.Z).ToList();
                                        // linear
                                        // can be a wall or line based family.
                                        if (obj.CategoryId == -2000011)
                                        {

                                            // draw a wall
                                            Curve crv = null;
                                            if (lbc.CurveType == "Line")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                crv = Line.CreateBound(pt1, pt2);
                                            }
                                            else if (lbc.CurveType == "Arc")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                crv = Arc.Create(pt1, pt3, pt2);
                                            }

                                            if (crv != null)
                                            {

                                                // Find the level
                                                Level lvl = GetLevel(lbc.ControlPoints, doc);

                                                double offset = 0;
                                                if (Math.Abs(UnitUtils.ConvertToInternalUnits(curvePoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                                {
                                                    offset = UnitUtils.ConvertToInternalUnits(curvePoints[0].Z, lengthDUT) - lvl.Elevation;
                                                }

                                                // Create the wall
                                                Wall w = null;
                                                try
                                                {
                                                    w = Wall.Create(doc, crv, wallType.Id, lvl.Id, 10.0, offset, false, false);
                                                }
                                                catch (Exception ex)
                                                {
                                                    TaskDialog.Show("ERROR", ex.Message);
                                                }

                                                // Assign the parameters
                                                SetParameters(w, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(w, uniqueId, instanceSchema, 0, nickName);
                                            }
                                        }
                                        // See if it's a structural column
                                        else if (obj.CategoryId == -2001330)
                                        {
                                            if (symbol != null && lbc.CurveType == "Line")
                                            {
                                                XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                Curve crv = Line.CreateBound(origin, pt2);

                                                // Find the level
                                                Level lvl = GetLevel(lbc.ControlPoints, doc);

                                                // Create the column
                                                fi = doc.Create.NewFamilyInstance(origin, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.Column);

                                                // Change it to a slanted column
                                                Parameter slantParam = fi.get_Parameter(BuiltInParameter.SLANTED_COLUMN_TYPE_PARAM);

                                                // SlantedOrVerticalColumnType has 3 options, CT_Vertical (0), CT_Angle (1), or CT_EndPoint (2)
                                                // CT_EndPoint is what we want for a line based column.
                                                slantParam.Set(2);

                                                // Set the location curve of the column to the line
                                                LocationCurve lc = fi.Location as LocationCurve;
                                                if (lc != null)
                                                {
                                                    lc.Curve = crv;
                                                }

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                            }
                                        }
                                        else if (obj.CategoryId == -2000220)
                                        {
                                            // draw a grid
                                            Grid g = null;
                                            if (lbc.CurveType == "Line")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                Line line = Line.CreateBound(pt1, pt2);
                                                try
                                                {
                                                    g = doc.Create.NewGrid(line);
                                                }
                                                catch { }
                                            }
                                            else if (lbc.CurveType == "Arc")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                Arc arc = Arc.Create(pt1, pt3, pt2);
                                                try
                                                {
                                                    g = doc.Create.NewGrid(arc);
                                                }
                                                catch { }
                                            }

                                            if (g != null)
                                            {
                                                g.ExtendToAllLevels(); ;
                                                // Assign the parameters
                                                SetParameters(g, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(g, uniqueId, instanceSchema, 0, nickName);
                                            }
                                        }
                                        else if (obj.CategoryId == -2000051)
                                        {
                                            // Draw a line
                                            Curve crv = null;
                                            Curve crv2 = null;

                                            if (lbc.CurveType == "Line")
                                            {
                                                XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                crv = Line.CreateBound(origin, pt2);
                                            }
                                            else if (lbc.CurveType == "Arc")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                crv = Arc.Create(pt1, pt3, pt2);
                                            }
                                            else if (lbc.CurveType == "Circle")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                XYZ pt4 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Z, lengthDUT));
                                                XYZ pt5 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Z, lengthDUT));
                                                Arc arc1 = Arc.Create(pt1, pt3, pt2);
                                                Arc arc2 = Arc.Create(pt3, pt5, pt4);
                                                crv = arc1;
                                                crv2 = arc2;
                                            }
                                            else if (lbc.CurveType == "Spline")
                                            {
                                                List<XYZ> controlPoints = new List<XYZ>();
                                                List<double> weights = lbc.Weights;
                                                List<double> knots = lbc.Knots;


                                                foreach (LyrebirdPoint lp in lbc.ControlPoints)
                                                {
                                                    XYZ pt = new XYZ(UnitUtils.ConvertToInternalUnits(lp.X, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Z, lengthDUT));
                                                    controlPoints.Add(pt);
                                                }

                                                NurbSpline spline;
                                                if (lbc.Degree < 3)
                                                    spline = NurbSpline.Create(controlPoints, weights);
                                                else
                                                    spline = NurbSpline.Create(controlPoints, weights, knots, lbc.Degree, false, true);

                                                crv = spline;
                                            }

                                            // Check for model or detail
                                            if (obj.FamilyName == "Model Lines")
                                            {
                                                // We need a plane
                                                
                                            }
                                            else if (obj.FamilyName == "Detail Lines")
                                            {
                                                // we need the active view.
                                            }
                                        }

                                        // Otherwise create a family it using the line
                                        else
                                        {
                                            if (symbol != null)
                                            {
                                                Curve crv = null;

                                                if (lbc.CurveType == "Line")
                                                {
                                                    XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                    XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                    crv = Line.CreateBound(origin, pt2);
                                                }
                                                else if (lbc.CurveType == "Arc")
                                                {
                                                    XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                    XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                    XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                    crv = Arc.Create(pt1, pt3, pt2);
                                                }
                                                // Find the level
                                                Level lvl = GetLevel(lbc.ControlPoints, doc);

                                                // Create the family
                                                if (symbol.Category.Id.IntegerValue == -2002000)
                                                {
                                                    try
                                                    {
                                                        Line line = crv as Line;
                                                        fi = doc.Create.NewFamilyInstance(line, symbol, doc.ActiveView);
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                else if (symbol.Category.Id.IntegerValue == -2001320)
                                                {
                                                    try
                                                    {
                                                        if (lbc.CurveType == "Arc")
                                                        {
                                                            XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                            fi = doc.Create.NewFamilyInstance(origin, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.Beam);
                                                            // Set the location curve of the column to the line
                                                            LocationCurve lc = fi.Location as LocationCurve;
                                                            if (lc != null)
                                                            {
                                                                lc.Curve = crv;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            fi = doc.Create.NewFamilyInstance(crv, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.Beam);
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                else
                                                {
                                                    try
                                                    {
                                                        fi = doc.Create.NewFamilyInstance(crv, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                            }
                                        }
                                    }
                                    #endregion

                                    #region Closed Curve Family
                                    else
                                    {
                                        // A list of curves.  These should equate a closed planar curve from GH.

                                        //TODO: For each profile type, determine if the offset is working correctly or inverted

                                        // Then determine category and create based on that.
                                        if (obj.CategoryId == -2000011)
                                        {
                                            // Create line based wall
                                            // Find the level
                                            double offset = 0;
                                            List<LyrebirdPoint> allPoints = new List<LyrebirdPoint>();
                                            foreach (LyrebirdCurve lc in obj.Curves)
                                            {
                                                foreach (LyrebirdPoint lp in lc.ControlPoints)
                                                {
                                                    allPoints.Add(lp);
                                                }
                                            }
                                            allPoints.Sort((x, y) => x.Z.CompareTo(y.Z));

                                            Level lvl = GetLevel(allPoints, doc);

                                            if (Math.Abs(UnitUtils.ConvertToInternalUnits(allPoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = UnitUtils.ConvertToInternalUnits(allPoints[0].Z, lengthDUT) - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            List<Curve> crvArray = new List<Curve>();
                                            try
                                            {
                                                foreach (LyrebirdCurve lbc in obj.Curves)
                                                {
                                                    if (lbc.CurveType == "Circle")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                        XYZ pt4 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Z, lengthDUT));
                                                        XYZ pt5 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Z, lengthDUT));
                                                        Arc arc1 = Arc.Create(pt1, pt3, pt2);
                                                        Arc arc2 = Arc.Create(pt3, pt5, pt4);
                                                        crvArray.Add(arc1);
                                                        crvArray.Add(arc2);
                                                    }
                                                    else if (lbc.CurveType == "Arc")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                        Arc arc = Arc.Create(pt1, pt3, pt2);
                                                        crvArray.Add(arc);
                                                    }
                                                    else if (lbc.CurveType == "Line")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        Line line = Line.CreateBound(pt1, pt2);
                                                        crvArray.Add(line);
                                                    }
                                                    else if (lbc.CurveType == "Spline")
                                                    {
                                                        List<XYZ> controlPoints = new List<XYZ>();
                                                        List<double> weights = lbc.Weights;
                                                        List<double> knots = lbc.Knots;


                                                        foreach (LyrebirdPoint lp in lbc.ControlPoints)
                                                        {
                                                            XYZ pt = new XYZ(UnitUtils.ConvertToInternalUnits(lp.X, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Z, lengthDUT));
                                                            controlPoints.Add(pt);
                                                        }

                                                        NurbSpline spline;
                                                        if (lbc.Degree < 3)
                                                            spline = NurbSpline.Create(controlPoints, weights);
                                                        else
                                                            spline = NurbSpline.Create(controlPoints, weights, knots, lbc.Degree, false, true);

                                                        crvArray.Add(spline);
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                TaskDialog.Show("ERROR", ex.Message);
                                            }

                                            // Create the floor
                                            Wall w = Wall.Create(doc, crvArray, wallType.Id, lvl.Id, false);
                                            if (Math.Abs(offset - 0) > double.Epsilon)
                                            {
                                                Parameter p = w.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
                                                p.Set(offset);
                                            }

                                            // Assign the parameters
                                            SetParameters(w, obj.Parameters, doc);

                                            // Assign the GH InstanceGuid
                                            AssignGuid(w, uniqueId, instanceSchema, 0, nickName);

                                        }
                                        else if (obj.CategoryId == -2000032)
                                        {
                                            // Create a profile based floor
                                            // Find the level
                                            Level lvl = GetLevel(obj.Curves[0].ControlPoints, doc);

                                            double offset = 0;
                                            if (Math.Abs(UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            CurveArray crvArray;
                                            Floor flr;
                                            List<Opening> flrOpenings = new List<Opening>();
                                            if (obj.CurveIds != null && obj.CurveIds.Count > 0)
                                            {
                                                // get the main profile
                                                int crvCount = obj.CurveIds[0];
                                                List<LyrebirdCurve> primaryCurves = obj.Curves.GetRange(0, crvCount);
                                                crvArray = GetCurveArray(primaryCurves);
                                                flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);

                                                // TODO: You cannot create holes in an element with the API without using openings rather than interior closed curves.
                                                // Evaluate with later versions if it's worth creating the openings and updating them
                                                // Create the openings associated with it.
                                                //int start = crvCount - 1;
                                                //for (int i = 1; i < obj.CurveIds.Count; i++)
                                                //{
                                                //    List<LyrebirdCurve> interiorCurves = obj.Curves.GetRange(start, obj.CurveIds[i]);
                                                //    start += interiorCurves.Count;
                                                //    CurveArray openingArray = GetCurveArray(interiorCurves);
                                                //    try
                                                //    {
                                                //        SubTransaction st2 = new SubTransaction(doc);
                                                //        st2.Start();
                                                //        Opening opening = doc.Create.NewOpening(flr, openingArray, false);
                                                //        st2.Commit();
                                                //        flrOpenings.Add(opening);

                                                //    }
                                                //    catch (Exception ex)
                                                //    {
                                                //        TaskDialog.Show("TEST", ex.Message);
                                                //    }
                                                //}
                                            }
                                            else
                                            {
                                                crvArray = GetCurveArray(obj.Curves);
                                                flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);
                                            }

                                            // Create the floor
                                            //flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);

                                            if (Math.Abs(offset - 0) > double.Epsilon)
                                            {
                                                Parameter p = flr.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM);
                                                p.Set(offset);
                                            }

                                            // Assign the parameters
                                            SetParameters(flr, obj.Parameters, doc);

                                            // Assign the GH InstanceGuid
                                            AssignGuid(flr, uniqueId, instanceSchema, 0, nickName);

                                        }
                                        else if (obj.CategoryId == -2000035)
                                        {
                                            // Create a RoofExtrusion
                                            // Find the level
                                            Level lvl = GetLevel(obj.Curves[0].ControlPoints, doc);

                                            double offset = 0;
                                            if (Math.Abs(UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            CurveArray crvArray = GetCurveArray(obj.Curves);

                                            // Create the roof
                                            FootPrintRoof roof = null;
                                            ModelCurveArray roofProfile = new ModelCurveArray();
                                            try
                                            {
                                                roof = doc.Create.NewFootPrintRoof(crvArray, lvl, roofType, out roofProfile);
                                            }
                                            catch (Exception ex)
                                            {
                                                TaskDialog.Show("ERROR", ex.Message);
                                            }
                                            if (Math.Abs(offset - 0) > double.Epsilon)
                                            {
                                                Parameter p = roof.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM);
                                                p.Set(offset);
                                            }

                                            // Assign the parameters
                                            SetParameters(roof, obj.Parameters, doc);

                                            // Assign the GH InstanceGuid
                                            AssignGuid(roof, uniqueId, instanceSchema, 0, nickName);
                                        }
                                    }
                                    #endregion
                                }
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error", ex.ToString());
                                Debug.WriteLine(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                        t.Commit();
                    }
                }


            }
            #endregion
        }
Example #28
0
        public override FScheme.Value Evaluate(FSharpList <FScheme.Value> args)
        {
            var doc          = dynRevitSettings.Doc;
            var refCurveList = ((FScheme.Value.List)args[0]).Item.Select(
                x => (((FScheme.Value.Container)x).Item is Autodesk.Revit.DB.ModelCurve ?
                      ((Autodesk.Revit.DB.ModelCurve)((FScheme.Value.Container)x).Item)
                   : (Autodesk.Revit.DB.ModelCurve)(
                          doc.Document.GetElement(
                              ((Reference)((FScheme.Value.Container)x).Item).ElementId)
                          )
                      )
                ).ToList();

            var myModelCurves = new ModelCurveArray();

            //Plane thisPlane = null;
            //Line oneLine = null;

            var    refIds    = new List <ElementId>();
            var    loopStart = new XYZ();
            var    otherEnd  = new XYZ();
            int    index     = 0;
            double tolerance = 0.000000001;

            foreach (var refCurve in refCurveList)
            {
                if (index == 0)
                {
                    loopStart = refCurve.GeometryCurve.Evaluate(0.0, true);
                    otherEnd  = refCurve.GeometryCurve.Evaluate(1.0, true);
                }
                else //if (index > 0)
                {
                    XYZ startXYZ = refCurve.GeometryCurve.Evaluate(0.0, true);
                    XYZ endXYZ   = refCurve.GeometryCurve.Evaluate(1.0, true);
                    if (index == 1)
                    {
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) > tolerance &&
                            (startXYZ.DistanceTo(loopStart) > tolerance || endXYZ.DistanceTo(loopStart) > tolerance))
                        {
                            XYZ temp = loopStart;
                            loopStart = otherEnd;
                            otherEnd  = temp;
                        }
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) < tolerance)
                        {
                            otherEnd = startXYZ;
                        }
                        else if (startXYZ.DistanceTo(otherEnd) < tolerance && endXYZ.DistanceTo(otherEnd) > tolerance)
                        {
                            otherEnd = endXYZ;
                        }
                        else
                        {
                            throw new Exception("Gap between curves in chain of reference curves.");
                        }
                    }
                }

                refIds.Add(refCurve.Id);
                myModelCurves.Append(refCurve);
                index++;
            }

            List <ElementId> removeIds = new List <ElementId>();

            foreach (ElementId oldId in this.Elements)
            {
                if (!refIds.Contains(oldId))
                {
                    removeIds.Add(oldId);
                }
            }

            foreach (ElementId removeId in removeIds)
            {
                this.Elements.Remove(removeId);
            }
            foreach (ElementId newId in refIds)
            {
                if (!this.Elements.Contains(newId))
                {
                    this.Elements.Add(newId);
                }
            }
            //if (!curveLoop.HasPlane())
            //    throw new Exception(" Planar Ref Curve Chain fails: not planar");
            return(FScheme.Value.NewContainer(myModelCurves));
        }
Example #29
0
        public override Value Evaluate(FSharpList <Value> args)
        {
            var doc          = dynRevitSettings.Doc;
            var refCurveList = ((Value.List)args[0]).Item.Select(
                x => (((Value.Container)x).Item is ModelCurve ?
                      ((ModelCurve)((Value.Container)x).Item)
                   : (ModelCurve)(
                          doc.Document.GetElement(
                              ((Reference)((Value.Container)x).Item).ElementId)
                          )
                      )
                ).ToList();

            ModelCurveArray myModelCurves = new ModelCurveArray();

            //Plane thisPlane = null;
            //Line oneLine = null;

            List <ElementId> refIds = new List <ElementId>();
            XYZ    loopStart        = new XYZ();
            XYZ    otherEnd         = new XYZ();
            int    index            = 0;
            double tolerance        = 0.000000001;

            foreach (var refCurve in refCurveList)
            {
                if (index == 0)
                {
                    loopStart = refCurve.GeometryCurve.Evaluate(0.0, true);
                    otherEnd  = refCurve.GeometryCurve.Evaluate(1.0, true);
                }
                else //if (index > 0)
                {
                    XYZ startXYZ = refCurve.GeometryCurve.Evaluate(0.0, true);
                    XYZ endXYZ   = refCurve.GeometryCurve.Evaluate(1.0, true);
                    if (index == 1)
                    {
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) > tolerance &&
                            (startXYZ.DistanceTo(loopStart) > tolerance || endXYZ.DistanceTo(loopStart) > tolerance))
                        {
                            XYZ temp = loopStart;
                            loopStart = otherEnd;
                            otherEnd  = temp;
                        }
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) < tolerance)
                        {
                            otherEnd = startXYZ;
                        }
                        else if (startXYZ.DistanceTo(otherEnd) < tolerance && endXYZ.DistanceTo(otherEnd) > tolerance)
                        {
                            otherEnd = endXYZ;
                        }
                        else
                        {
                            throw new Exception("Gap between curves in chain of reference curves.");
                        }
                    }
                }

                /* not needed check
                 * if (refCurve.GeometryCurve is Line)
                 * {
                 *  Line thisLine = refCurve.GeometryCurve as Line;
                 *  if (thisPlane != null)
                 *  {
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Direction)) > tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Origin - thisPlane.Origin)) > tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *  }
                 *  else if (oneLine == null)
                 *      oneLine = thisLine;
                 *  else
                 *  {
                 *      if (Math.Abs(oneLine.Direction.DotProduct(thisLine.Direction)) > 1.0 - tolerance)
                 *      {
                 *          double projAdjust = oneLine.Direction.DotProduct(oneLine.Origin - thisLine.Origin);
                 *          XYZ adjustedOrigin = thisLine.Origin + projAdjust * oneLine.Direction;
                 *          if (adjustedOrigin.DistanceTo(oneLine.Origin) > tolerance)
                 *              throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *      }
                 *      else
                 *      {
                 *          XYZ norm = oneLine.Direction.CrossProduct(thisLine.Direction);
                 *          norm = norm.Normalize();
                 *          thisPlane = new Plane(norm, oneLine.Origin);
                 *          if (Math.Abs(thisPlane.Normal.DotProduct(thisLine.Origin - thisPlane.Origin)) > tolerance)
                 *              throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *      }
                 *
                 *  }
                 * }
                 * else
                 * {
                 *  CurveLoop curveLoop = new CurveLoop();
                 *  curveLoop.Append(refCurve.GeometryCurve);
                 *  if (!curveLoop.HasPlane())
                 *      throw new Exception(" Planar Ref Curve Chain fails: curve is not planar.");
                 *  Plane curvePlane = curveLoop.GetPlane();
                 *  if (thisPlane == null && oneLine == null)
                 *      thisPlane = curveLoop.GetPlane();
                 *  else if (thisPlane != null)
                 *  {
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(curvePlane.Normal)) < 1.0 - tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(curvePlane.Origin - thisPlane.Origin)) > tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *  }
                 *  else if (oneLine != null)
                 *  {
                 *      thisPlane = curvePlane;
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(oneLine.Direction)) > tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *      if (Math.Abs(thisPlane.Normal.DotProduct(oneLine.Origin - thisPlane.Origin)) > tolerance)
                 *          throw new Exception(" Planar Ref Curve Chain fails: not planar");
                 *  }
                 * }
                 */

                refIds.Add(refCurve.Id);
                myModelCurves.Append(refCurve);
                index++;
            }

            List <ElementId> removeIds = new List <ElementId>();

            foreach (ElementId oldId in this.Elements)
            {
                if (!refIds.Contains(oldId))
                {
                    removeIds.Add(oldId);
                }
            }

            foreach (ElementId removeId in removeIds)
            {
                this.Elements.Remove(removeId);
            }
            foreach (ElementId newId in refIds)
            {
                if (!this.Elements.Contains(newId))
                {
                    this.Elements.Add(newId);
                }
            }
            //if (!curveLoop.HasPlane())
            //    throw new Exception(" Planar Ref Curve Chain fails: not planar");
            return(Value.NewContainer(myModelCurves));
        }
        void f(Document doc)
        {
            // Before invoking this sample, select some walls
            // to add a roof over. Make sure there is a level
            // named "Roof" in the document.

            Level level
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(Level))
                  .Where <Element>(e =>
                                   !string.IsNullOrEmpty(e.Name) &&
                                   e.Name.Equals("Roof"))
                  .FirstOrDefault <Element>() as Level;

            RoofType roofType
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(RoofType))
                  .FirstOrDefault <Element>() as RoofType;

            // Get the handle of the application
            Application application = doc.Application;

            // Define the footprint for the roof based on user selection
            CurveArray footprint = application.Create
                                   .NewCurveArray();

            UIDocument uidoc = new UIDocument(doc);

            ICollection <ElementId> selectedIds
                = uidoc.Selection.GetElementIds();

            if (selectedIds.Count != 0)
            {
                foreach (ElementId id in selectedIds)
                {
                    Element element = doc.GetElement(id);
                    Wall    wall    = element as Wall;
                    if (wall != null)
                    {
                        LocationCurve wallCurve = wall.Location as LocationCurve;
                        footprint.Append(wallCurve.Curve);
                        continue;
                    }

                    ModelCurve modelCurve = element as ModelCurve;
                    if (modelCurve != null)
                    {
                        footprint.Append(modelCurve.GeometryCurve);
                    }
                }
            }
            else
            {
                throw new Exception(
                          "Please select a curve loop, wall loop or "
                          + "combination of walls and curves to "
                          + "create a footprint roof.");
            }

            ModelCurveArray footPrintToModelCurveMapping
                = new ModelCurveArray();

            FootPrintRoof footprintRoof
                = doc.Create.NewFootPrintRoof(
                      footprint, level, roofType,
                      out footPrintToModelCurveMapping);

            ModelCurveArrayIterator iterator
                = footPrintToModelCurveMapping.ForwardIterator();

            iterator.Reset();
            while (iterator.MoveNext())
            {
                ModelCurve modelCurve = iterator.Current as ModelCurve;
                footprintRoof.set_DefinesSlope(modelCurve, true);
                footprintRoof.set_SlopeAngle(modelCurve, 0.5);
            }
        }
        /// <summary>
        /// Create other lines, including Ellipse, HermiteSpline and NurbSpline
        /// </summary>
        /// <param name="sketchId">the id of the sketch plane</param>
        /// <param name="elementId">the element id which copy the curve from</param>
        /// <param name="offsetPoint">the offset direction from the copied line</param>
        public void CreateOthers(ElementId sketchId, ElementId elementId, Autodesk.Revit.DB.XYZ offsetPoint)
        {
            // First get the sketch plane by the giving element id.
            SketchPlane workPlane = GetSketchPlaneById(sketchId);

            // Because the geometry of these lines can't be created by API,
            // use an existing geometry to create ModelEllipse, ModelHermiteSpline, ModelNurbSpline
            // and then move a bit to make the user see the creation distinctly

            // This method use NewModelCurveArray() method to create model lines
            CurveArray curves = m_createApp.NewCurveArray();// create a geometry curve array

            // Get the Autodesk.Revit.DB.ElementId which used to get the corresponding element
            ModelCurve selected = GetElementById(elementId) as ModelCurve;

            if (null == selected)
            {
                throw new Exception("Don't have the element you select");
            }

            // add the geometry curve of the element
            curves.Append(selected.GeometryCurve); // add the geometry ellipse

            // Create the model line
            ModelCurveArray modelCurves = m_createDoc.NewModelCurveArray(curves, workPlane);

            if (null == modelCurves || 1 != modelCurves.Size) // assert the creation is successful
            {
                throw new Exception("Create the ModelCurveArray failed.");
            }

            // Offset the create model lines in order to differentiate the existing model lines
            foreach (ModelCurve m in modelCurves)
            {
                ElementTransformUtils.MoveElement(m.Document, m.Id, offsetPoint); // move the lines
            }
            // Add the created model lines into corresponding array
            foreach (ModelCurve m in modelCurves)
            {
                switch (m.GetType().Name)
                {
                case "ModelEllipse":          // If the line is Ellipse
                    m_ellipseArray.Append(m); // Add to Ellipse array
                    break;

                case "ModelHermiteSpline":    // If the line is HermiteSpline
                    m_hermiteArray.Append(m); // Add to HermiteSpline array
                    break;

                case "ModelNurbSpline":       // If the line is NurbSpline
                    m_nurbArray.Append(m);    // Add to NurbSpline
                    break;

                default:
                    break;
                }
            }

            // Finally refresh information map.
            RefreshInformationMap();
        }
Example #32
0
        /***************************************************/
        /****              Public methods               ****/
        /***************************************************/

        public static RoofBase ToRevitRoofBase(this oM.Physical.Elements.Roof roof, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null)
        {
            if (roof == null || roof.Location == null || document == null)
            {
                return(null);
            }

            PlanarSurface planarSurface = roof.Location as PlanarSurface;

            if (planarSurface == null)
            {
                return(null);
            }

            RoofBase roofBase = refObjects.GetValue <RoofBase>(document, roof.BHoM_Guid);

            if (roofBase != null)
            {
                return(roofBase);
            }

            settings = settings.DefaultIfNull();

            RoofType roofType = roof.Construction?.ToRevitElementType(document, new List <BuiltInCategory> {
                BuiltInCategory.OST_Roofs
            }, settings, refObjects) as RoofType;

            if (roofType == null)
            {
                roofType = roof.ElementType(document, settings);
            }

            if (roofType == null)
            {
                Compute.ElementTypeNotFoundWarning(roof);
                return(null);
            }

            Level level = document.LevelBelow(roof.Location, settings);

            if (level == null)
            {
                return(null);
            }

            double elevation = level.Elevation.ToSI(UnitType.UT_Length);

            oM.Geometry.Plane plane = BH.Engine.Geometry.Create.Plane(BH.Engine.Geometry.Create.Point(0, 0, elevation), BH.Engine.Geometry.Create.Vector(0, 0, 1));

            ICurve     curve      = planarSurface.ExternalBoundary.IProject(plane);
            CurveArray curveArray = Create.CurveArray(curve.IToRevitCurves());

            ModelCurveArray modelCurveArray = new ModelCurveArray();

            roofBase = document.Create.NewFootPrintRoof(curveArray, level, roofType, out modelCurveArray);

            if (roofBase != null)
            {
                Parameter parameter = roofBase.get_Parameter(BuiltInParameter.ROOF_UPTO_LEVEL_PARAM);
                if (parameter != null)
                {
                    parameter.Set(ElementId.InvalidElementId);
                }

                List <oM.Geometry.Point> controlPoints = planarSurface.ExternalBoundary.IControlPoints();

                if (controlPoints != null && controlPoints.Count > 2)
                {
                    SlabShapeEditor slabShapeEditor = roofBase.SlabShapeEditor;
                    slabShapeEditor.ResetSlabShape();

                    foreach (oM.Geometry.Point point in controlPoints)
                    {
                        if (Math.Abs(point.Z - plane.Origin.Z) > settings.DistanceTolerance)
                        {
                            XYZ xyz = point.ToRevit();
                            slabShapeEditor.DrawPoint(xyz);
                        }
                    }
                }
            }

            roofBase.CheckIfNullPush(roof);
            if (roofBase == null)
            {
                return(null);
            }

            // Copy parameters from BHoM object to Revit element
            roofBase.CopyParameters(roof, settings);

            // Update the offset in case the level had been overwritten.
            double offset = 0;

            if (roofBase.LevelId.IntegerValue != level.Id.IntegerValue)
            {
                Level newLevel = document.GetElement(roofBase.LevelId) as Level;
                offset += (level.ProjectElevation - newLevel.ProjectElevation).ToSI(UnitType.UT_Length);
            }

            roofBase.SetParameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM, offset);

            refObjects.AddOrReplace(roof, roofBase);
            return(roofBase);
        }
Example #33
0
        public static HostObject ToRevit(this Panel panel, Document document, Core.Revit.ConvertSettings convertSettings)
        {
            Geometry.Spatial.Face3D face3D = panel?.GetFace3D();
            if (face3D == null)
            {
                return(null);
            }

            HostObject result = convertSettings?.GetObject <HostObject>(panel.Guid);

            if (result != null)
            {
                return(result);
            }

            PanelType panelType = panel.PanelType;

            Geometry.Spatial.Vector3D normal = panel.Normal;

            HostObjAttributes hostObjAttributes = panel.Construction.ToRevit(document, panelType, normal, convertSettings);

            if (hostObjAttributes == null)
            {
                hostObjAttributes = Analytical.Query.DefaultConstruction(panelType)?.ToRevit(document, panelType, normal, convertSettings); //Default Construction
            }
            BuiltInParameter[] builtInParameters = null;
            if (hostObjAttributes is Autodesk.Revit.DB.WallType)
            {
                double lowElevation = panel.LowElevation();

                Level level = document.LowLevel(lowElevation);

                Autodesk.Revit.DB.Wall wall = ToRevit_Wall(face3D, document, (Autodesk.Revit.DB.WallType)hostObjAttributes, level);
                if (wall == null)
                {
                    return(result);
                }

                //List<Curve> curveList = new List<Curve>();
                //foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds())
                //{
                //    if (Geometry.Spatial.Query.Clockwise(closedPlanar3D))
                //        closedPlanar3D.Reverse();

                //    List<Line> lines = closedPlanar3D.ToRevit();
                //    if (lines == null)
                //        continue;

                //    curveList.AddRange(lines);
                //}

                //if (curveList == null || curveList.Count == 0)
                //    return null;

                //double lowElevation = panel.LowElevation();

                //Level level = document.LowLevel(lowElevation);
                //if (level == null)
                //    return null;

                //Wall wall = Wall.Create(document, curveList, hostObjAttributes.Id, level.Id, false, panel.Normal.ToRevit(false));

                Parameter parameter = null;

                parameter = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE);
                if (parameter != null)
                {
                    parameter.Set(ElementId.InvalidElementId);
                }

                parameter = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM);
                if (parameter != null)
                {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                    double height = UnitUtils.ConvertToInternalUnits((panel.HighElevation() - lowElevation), DisplayUnitType.DUT_METERS);
#else
                    double height = UnitUtils.ConvertToInternalUnits((panel.HighElevation() - lowElevation), UnitTypeId.Meters);
#endif


                    parameter.Set(height);
                }

#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                double levelElevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS);
#else
                double levelElevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, UnitTypeId.Meters);
#endif

                if (Math.Abs(lowElevation - levelElevation) > Core.Tolerance.MacroDistance)
                {
                    parameter = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
                    if (parameter != null)
                    {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                        parameter.Set(UnitUtils.ConvertToInternalUnits(lowElevation - levelElevation, DisplayUnitType.DUT_METERS));
#else
                        parameter.Set(UnitUtils.ConvertToInternalUnits(lowElevation - levelElevation, UnitTypeId.Meters));
#endif
                    }
                }

                builtInParameters = new BuiltInParameter[] { BuiltInParameter.WALL_BASE_CONSTRAINT, BuiltInParameter.WALL_BASE_OFFSET, BuiltInParameter.WALL_HEIGHT_TYPE, BuiltInParameter.WALL_USER_HEIGHT_PARAM, BuiltInParameter.WALL_KEY_REF_PARAM };
                result            = wall;
            }
            else if (hostObjAttributes is Autodesk.Revit.DB.FloorType)
            {
                Geometry.Spatial.IClosedPlanar3D closedPlanar3D_External = face3D.GetExternalEdge3D();
                if (!(closedPlanar3D_External is Geometry.Spatial.ICurvable3D))
                {
                    return(null);
                }

                double elevation = panel.LowElevation();
                Level  level     = document.HighLevel(elevation);

                Geometry.Spatial.Plane plane = new Geometry.Spatial.Plane(new Geometry.Spatial.Point3D(0, 0, elevation), Geometry.Spatial.Vector3D.WorldZ);

                CurveArray curveArray_Sloped = new CurveArray();
                CurveArray curveArray_Plane  = new CurveArray();

                Geometry.Spatial.IClosedPlanar3D closedPlanar3D = face3D.GetExternalEdge3D();
                if (!(closedPlanar3D is Geometry.Spatial.ICurvable3D))
                {
                    return(null);
                }

                List <Geometry.Spatial.Segment3D> segment3Ds = Geometry.Revit.Query.Segment3Ds(closedPlanar3D);
                if (segment3Ds == null || segment3Ds.Count == 0)
                {
                    return(null);
                }

                foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds)
                {
                    curveArray_Sloped.Append(segment3D.ToRevit_Line());

                    Geometry.Spatial.Segment3D segment3D_Temp = Geometry.Spatial.Query.Project(plane, segment3D);
                    if (segment3D_Temp == null)
                    {
                        continue;
                    }

                    curveArray_Plane.Append(segment3D_Temp.ToRevit_Line());
                }

#if Revit2017 || Revit2018 || Revit2019 || Revit2020 || Revit2021
                Autodesk.Revit.DB.Floor floor = document.Create.NewFloor(curveArray_Plane, hostObjAttributes as Autodesk.Revit.DB.FloorType, level, false);
#else
                CurveLoop curveLoop = new CurveLoop();
                foreach (Curve curve in curveArray_Plane)
                {
                    curveLoop.Append(curve);
                }

                Autodesk.Revit.DB.Floor floor = Autodesk.Revit.DB.Floor.Create(document, new CurveLoop[] { curveLoop }, hostObjAttributes.Id, level.Id);
#endif

                if (floor != null)
                {
                    floor.ChangeTypeId(hostObjAttributes.Id);

                    List <Geometry.Spatial.IClosedPlanar3D> closedPlanar3Ds_Internal = face3D.GetInternalEdge3Ds();
                    if (closedPlanar3Ds_Internal != null && closedPlanar3Ds_Internal.Count > 0)
                    {
                        //Requires to be regenerated before inserting openings
                        //https://thebuildingcoder.typepad.com/blog/2013/07/create-a-floor-with-an-opening-or-complex-boundary.html
                        document.Regenerate();

                        foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D_Internal in face3D.GetInternalEdge3Ds())
                        {
                            List <Geometry.Spatial.Segment3D> segment3Ds_Internal = Geometry.Revit.Query.Segment3Ds(closedPlanar3D_Internal);
                            if (segment3Ds_Internal == null || segment3Ds_Internal.Count == 0)
                            {
                                continue;
                            }

                            curveArray_Plane = new CurveArray();
                            //foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds)
                            foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds_Internal)
                            {
                                curveArray_Sloped.Append(segment3D.ToRevit_Line());

                                Geometry.Spatial.Segment3D segment3D_Temp = Geometry.Spatial.Query.Project(plane, segment3D);
                                if (segment3D_Temp == null)
                                {
                                    continue;
                                }

                                curveArray_Plane.Append(segment3D_Temp.ToRevit_Line());
                            }

                            Opening opening = document.Create.NewOpening(floor, curveArray_Plane, true);
                        }
                    }
                }

                if (floor != null)
                {
                    document.Regenerate();

                    SlabShapeEditor slabShapeEditor = floor.SlabShapeEditor;
                    if (slabShapeEditor != null)
                    {
                        slabShapeEditor.ResetSlabShape();

                        foreach (Curve curve in curveArray_Sloped)
                        {
                            XYZ xYZ = curve.GetEndPoint(0);
                            slabShapeEditor.DrawPoint(xYZ);
                        }
                    }
                }

                builtInParameters = new BuiltInParameter[] { BuiltInParameter.LEVEL_PARAM };
                result            = floor;
            }
            else if (hostObjAttributes is Autodesk.Revit.DB.RoofType)
            {
                CurveArray curveArray = new CurveArray();
                foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds())
                {
                    List <Geometry.Spatial.Segment3D> segment3Ds = Geometry.Revit.Query.Segment3Ds(closedPlanar3D);
                    if (segment3Ds == null || segment3Ds.Count == 0)
                    {
                        return(null);
                    }

                    segment3Ds.ForEach(x => curveArray.Append(x.ToRevit_Line()));
                }

                Level  level          = document.HighLevel(panel.LowElevation());
                double levelElevation = level.Elevation;

                ModelCurveArray modelCurveArray = new ModelCurveArray();
                RoofBase        roofBase        = document.Create.NewFootPrintRoof(curveArray, level, hostObjAttributes as Autodesk.Revit.DB.RoofType, out modelCurveArray);

                Parameter parameter = roofBase.get_Parameter(BuiltInParameter.ROOF_UPTO_LEVEL_PARAM);
                if (parameter != null)
                {
                    parameter.Set(ElementId.InvalidElementId);
                }

                SlabShapeEditor slabShapeEditor = roofBase.SlabShapeEditor;
                if (slabShapeEditor != null)
                {
                    slabShapeEditor.ResetSlabShape();

                    foreach (Curve curve in curveArray)
                    {
                        XYZ xYZ = curve.GetEndPoint(0);
                        //if (Math.Abs(xYZ.Z - levelElevation) > Core.Tolerance.MicroDistance)
                        slabShapeEditor.DrawPoint(xYZ);
                    }
                }

                builtInParameters = new BuiltInParameter[] { BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM, BuiltInParameter.ROOF_BASE_LEVEL_PARAM, BuiltInParameter.ROOF_UPTO_LEVEL_PARAM };
                result            = roofBase;
            }

            if (result == null)
            {
                return(null);
            }

            List <Aperture> apertures = panel.Apertures;
            if (apertures != null)
            {
                if (result is Autodesk.Revit.DB.Wall && ((Autodesk.Revit.DB.Wall)result).WallType.Kind == WallKind.Curtain)
                {
                }
                else
                {
                    foreach (Aperture aperture in apertures)
                    {
                        Geometry.Spatial.Plane plane_Aperture = aperture?.PlanarBoundary3D?.Plane;
                        if (plane_Aperture == null)
                        {
                            continue;
                        }

                        //bool flipHand = !plane_Panel.AxisX.SameHalf(plane_Aperture.AxisX);
                        //bool flipFacing = !plane_Panel.Normal.SameHalf(plane_Aperture.Normal);

                        FamilyInstance failyInstance_Aperture = aperture.ToRevit(document, result, convertSettings);
                    }
                }
            }

            if (convertSettings.ConvertParameters)
            {
                Core.Revit.Modify.SetValues(result, panel, builtInParameters);
                Core.Revit.Modify.SetValues(result, panel, ActiveSetting.Setting);

                Core.Revit.Modify.SetJson(result, panel.ToJObject()?.ToString());
            }
            //TODO: Implement proper log
            //System.IO.File.AppendAllText(@"C:\Users\DengusiakM\Desktop\SAM\2020-04-16 floorbug\LOG.txt", string.Format("{0}\t{1}\t{2}\n", DateTime.Now.ToString(), panel.Guid, panel.Name));

            convertSettings?.Add(panel.Guid, result);

            return(result);
        }
Example #34
0
        public override FScheme.Value Evaluate(FSharpList<FScheme.Value> args)
        {
            var doc = dynRevitSettings.Doc;
            var refCurveList = ((FScheme.Value.List)args[0]).Item.Select(
               x => (((FScheme.Value.Container)x).Item is Autodesk.Revit.DB.ModelCurve ?
                   ((Autodesk.Revit.DB.ModelCurve)((FScheme.Value.Container)x).Item)
                   : (Autodesk.Revit.DB.ModelCurve)(
                                      doc.Document.GetElement(
                                             ((Reference)((FScheme.Value.Container)x).Item).ElementId)
                                                             )
                                 )
                   ).ToList();

            var myModelCurves = new ModelCurveArray();

            //Plane thisPlane = null;
            //Line oneLine = null;

            var refIds = new List<ElementId>();
            var loopStart = new XYZ();
            var otherEnd = new XYZ();
            int index = 0;
            double tolerance = 0.000000001;
            foreach (var refCurve in refCurveList)
            {
                if (index == 0)
                {
                    loopStart = refCurve.GeometryCurve.Evaluate(0.0, true);
                    otherEnd = refCurve.GeometryCurve.Evaluate(1.0, true);
                }
                else //if (index > 0)
                {
                    XYZ startXYZ = refCurve.GeometryCurve.Evaluate(0.0, true);
                    XYZ endXYZ = refCurve.GeometryCurve.Evaluate(1.0, true);
                    if (index == 1)
                    {
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) > tolerance &&
                            (startXYZ.DistanceTo(loopStart) > tolerance || endXYZ.DistanceTo(loopStart) > tolerance))
                        {
                            XYZ temp = loopStart;
                            loopStart = otherEnd;
                            otherEnd = temp;
                        }
                        if (startXYZ.DistanceTo(otherEnd) > tolerance && endXYZ.DistanceTo(otherEnd) < tolerance)
                            otherEnd = startXYZ;
                        else if (startXYZ.DistanceTo(otherEnd) < tolerance && endXYZ.DistanceTo(otherEnd) > tolerance)
                            otherEnd = endXYZ;
                        else
                            throw new Exception("Gap between curves in chain of reference curves.");
                    }
                }

                refIds.Add(refCurve.Id);
                myModelCurves.Append(refCurve);
                index++;
            }

            List<ElementId> removeIds = new List<ElementId>();
            foreach (ElementId oldId in this.Elements)
            {
                if (!refIds.Contains(oldId))
                {
                    removeIds.Add(oldId);
                }
            }

            foreach (ElementId removeId in removeIds)
            {
                this.Elements.Remove(removeId);
            }
            foreach (ElementId newId in refIds)
            {
                if (!this.Elements.Contains(newId))
                    this.Elements.Add(newId);
            }
            //if (!curveLoop.HasPlane())
            //    throw new Exception(" Planar Ref Curve Chain fails: not planar");
            return FScheme.Value.NewContainer(myModelCurves);
        }
Example #35
0
        private void ModifyObjects(List<RevitObject> existingObjects, List<ElementId> existingElems, Document doc, Guid uniqueId, bool profileWarning, string nickName, int runId)
        {
            // Create new Revit objects.
            //List<LyrebirdId> newUniqueIds = new List<LyrebirdId>();

            // Determine what kind of object we're creating.
            RevitObject ro = existingObjects[0];


            #region Normal Origin based FamilyInstance
            // Modify origin based family instances
            if (ro.Origin != null)
            {
                // Find the FamilySymbol
                FamilySymbol symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);

                LevelType lt = FindLevelType(ro.TypeName, doc);

                GridType gt = FindGridType(ro.TypeName, doc);

                if (symbol != null || lt != null)
                {
                    // Get the hosting ID from the family.
                    Family fam = null;
                    Parameter hostParam = null;
                    int hostBehavior = 0;

                    try
                    {
                        fam = symbol.Family;
                        hostParam = fam.get_Parameter(BuiltInParameter.FAMILY_HOSTING_BEHAVIOR);
                        hostBehavior = hostParam.AsInteger();
                    }
                    catch{}

                    //FamilyInstance existingInstance = doc.GetElement(existingElems[0]) as FamilyInstance;
                    
                    using (Transaction t = new Transaction(doc, "Lyrebird Modify Objects"))
                    {
                        t.Start();
                        try
                        {
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");
                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");
                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }

                            FamilyInstance fi = null;
                            XYZ origin = XYZ.Zero;

                            if (lt != null)
                            {
                                for (int i = 0; i < existingObjects.Count; i++)
                                {
                                    RevitObject obj = existingObjects[i];
                                    Level lvl = doc.GetElement(existingElems[i]) as Level;

                                    if (lvl.ProjectElevation != (UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT)))
                                    {
                                        double offset = lvl.Elevation - lvl.ProjectElevation;
                                        lvl.Elevation = (UnitUtils.ConvertToInternalUnits(obj.Origin.Z + offset, lengthDUT));
                                    }

                                    SetParameters(lvl, obj.Parameters, doc);
                                }
                            }
                            else if (hostBehavior == 0)
                            {

                                for (int i = 0; i < existingObjects.Count; i++)
                                {
                                    RevitObject obj = existingObjects[i];
                                    fi = doc.GetElement(existingElems[i]) as FamilyInstance;

                                    // Change the family and symbol if necessary
                                    if (fi != null && (fi.Symbol.Family.Name != symbol.Family.Name || fi.Symbol.Name != symbol.Name))
                                    {
                                        try
                                        {
                                            fi.Symbol = symbol;
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine(ex.Message);
                                        }
                                    }

                                    try
                                    {
                                        // Move family
                                        origin = new XYZ(UnitUtils.ConvertToInternalUnits(obj.Origin.X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT));
                                        if (fi != null)
                                        {
                                            LocationPoint lp = fi.Location as LocationPoint;
                                            if (lp != null)
                                            {
                                                XYZ oldLoc = lp.Point;
                                                XYZ translation = origin.Subtract(oldLoc);
                                                ElementTransformUtils.MoveElement(doc, fi.Id, translation);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        TaskDialog.Show("Error", ex.Message);
                                    }

                                    // Rotate
                                    if (obj.Orientation != null)
                                    {
                                        if (Math.Round(Math.Abs(obj.Orientation.Z - 0), 10) < double.Epsilon)
                                        {
                                            XYZ orientation = fi.FacingOrientation;
                                            orientation = orientation.Multiply(-1);
                                            XYZ incomingOrientation = new XYZ(obj.Orientation.X, obj.Orientation.Y, obj.Orientation.Z);
                                            XYZ normalVector = new XYZ(0, -1, 0);

                                            double currentAngle = 0;
                                            if (orientation.X < 0 && orientation.Y < 0)
                                            {
                                                currentAngle = (2 * Math.PI) - normalVector.AngleTo(orientation);
                                            }
                                            else if (orientation.Y == 0 && orientation.X < 0)
                                            {
                                                currentAngle = 1.5 * Math.PI;
                                            }
                                            else if (orientation.X < 0)
                                            {
                                                currentAngle = (Math.PI - normalVector.AngleTo(orientation)) + Math.PI;
                                            }
                                            else
                                            {
                                                currentAngle = normalVector.AngleTo(orientation);
                                            }

                                            double incomingAngle = 0;
                                            if (incomingOrientation.X < 0 && incomingOrientation.Y < 0)
                                            {
                                                incomingAngle = (2 * Math.PI) - normalVector.AngleTo(incomingOrientation);
                                            }
                                            else if (incomingOrientation.Y == 0 && incomingOrientation.X < 0)
                                            {
                                                incomingAngle = 1.5 * Math.PI;
                                            }
                                            else if (incomingOrientation.X < 0)
                                            {
                                                incomingAngle = (Math.PI - normalVector.AngleTo(incomingOrientation)) + Math.PI;
                                            }
                                            else
                                            {
                                                incomingAngle = normalVector.AngleTo(incomingOrientation);
                                            }
                                            double angle = incomingAngle - currentAngle;
                                            //TaskDialog.Show("Test", "CurrentAngle: " + currentAngle.ToString() + "\nIncoming Angle: " + incomingAngle.ToString() + "\nResulting Rotation: " + angle.ToString() +
                                            //    "\nFacingOrientation: " + orientation.ToString() + "\nIncoming Orientation: " + incomingOrientation.ToString());
                                            Line axis = Line.CreateBound(origin, origin + XYZ.BasisZ);
                                            ElementTransformUtils.RotateElement(doc, fi.Id, axis, angle);
                                        }
                                    }

                                    SetParameters(fi, obj.Parameters, doc);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < existingObjects.Count; i++)
                                {
                                    RevitObject obj = existingObjects[i];
                                    fi = doc.GetElement(existingElems[i]) as FamilyInstance;

                                    // Change the family and symbol if necessary
                                    if (fi.Symbol.Family.Name != symbol.Family.Name || fi.Symbol.Name != symbol.Name)
                                    {
                                        try
                                        {
                                            fi.Symbol = symbol;
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine(ex.Message);
                                        }
                                    }

                                    origin = new XYZ(UnitUtils.ConvertToInternalUnits(obj.Origin.X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.Origin.Z, lengthDUT));

                                    // Find the level
                                    List<LyrebirdPoint> lbPoints = new List<LyrebirdPoint> { obj.Origin };
                                    Level lvl = GetLevel(lbPoints, doc);

                                    // Get the host
                                    if (hostBehavior == 5)
                                    {
                                        // Face based family.  Find the face and create the element
                                        XYZ normVector = new XYZ(obj.Orientation.X, obj.Orientation.Y, obj.Orientation.Z);
                                        XYZ faceVector;
                                        if (obj.FaceOrientation != null)
                                        {
                                            faceVector = new XYZ(obj.FaceOrientation.X, obj.FaceOrientation.Y, obj.FaceOrientation.Z);
                                        }
                                        else
                                        {
                                            faceVector = XYZ.BasisZ;
                                        }
                                        Face face = FindFace(origin, normVector, doc);
                                        if (face != null)
                                        {
                                            if (face.Reference.ElementId == fi.HostFace.ElementId)
                                            {
                                                //fi = doc.Create.NewFamilyInstance(face, origin, faceVector, symbol);
                                                // Just move the host and update the parameters as needed.
                                                LocationPoint lp = fi.Location as LocationPoint;
                                                if (lp != null)
                                                {
                                                    XYZ oldLoc = lp.Point;
                                                    XYZ translation = origin.Subtract(oldLoc);
                                                    ElementTransformUtils.MoveElement(doc, fi.Id, translation);
                                                }

                                                SetParameters(fi, obj.Parameters, doc);
                                            }
                                            else
                                            {
                                                FamilyInstance origInst = fi;
                                                fi = doc.Create.NewFamilyInstance(face, origin, faceVector, symbol);

                                                foreach (Parameter p in origInst.Parameters)
                                                {
                                                    try
                                                    {
                                                        Parameter newParam = fi.get_Parameter(p.GUID);
                                                        if (newParam != null)
                                                        {
                                                            switch (newParam.StorageType)
                                                            {
                                                                case StorageType.Double:
                                                                    newParam.Set(p.AsDouble());
                                                                    break;
                                                                case StorageType.ElementId:
                                                                    newParam.Set(p.AsElementId());
                                                                    break;
                                                                case StorageType.Integer:
                                                                    newParam.Set(p.AsInteger());
                                                                    break;
                                                                case StorageType.String:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                                default:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                            }
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                // Delete the original instance of the family
                                                doc.Delete(origInst.Id);

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // typical hosted family.  Can be wall, floor, roof or ceiling.
                                        ElementId host = FindHost(origin, hostBehavior, doc);
                                        if (host != null)
                                        {
                                            if (host.IntegerValue != fi.Host.Id.IntegerValue)
                                            {
                                                // We'll have to recreate the element
                                                FamilyInstance origInst = fi;
                                                fi = doc.Create.NewFamilyInstance(origin, symbol, doc.GetElement(host), lvl, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                                                foreach (Parameter p in origInst.Parameters)
                                                {
                                                    try
                                                    {
                                                        Parameter newParam = fi.LookupParameter(p.Definition.Name);
                                                        if (newParam != null)
                                                        {
                                                            switch (newParam.StorageType)
                                                            {
                                                                case StorageType.Double:
                                                                    newParam.Set(p.AsDouble());
                                                                    break;
                                                                case StorageType.ElementId:
                                                                    newParam.Set(p.AsElementId());
                                                                    break;
                                                                case StorageType.Integer:
                                                                    newParam.Set(p.AsInteger());
                                                                    break;
                                                                case StorageType.String:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                                default:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                            }
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                // Delete the original instance of the family
                                                doc.Delete(origInst.Id);

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(fi, uniqueId, instanceSchema, runId, nickName);
                                            }

                                            else
                                            {
                                                // Just move the host and update the parameters as needed.
                                                LocationPoint lp = fi.Location as LocationPoint;
                                                if (lp != null)
                                                {
                                                    XYZ oldLoc = lp.Point;
                                                    XYZ translation = origin.Subtract(oldLoc);
                                                    ElementTransformUtils.MoveElement(doc, fi.Id, translation);
                                                }

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);
                                            }
                                        }
                                    }

                                    // Assign the parameters
                                    SetParameters(fi, obj.Parameters, doc);
                                }
                                // delete the host finder
                                ElementId hostFinderFamily = hostFinder.Symbol.Family.Id;
                                doc.Delete(hostFinder.Id);
                                doc.Delete(hostFinderFamily);
                                hostFinder = null;
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }

                        t.Commit();
                    }
                }
            }

            #endregion


            #region Adaptive Components
            FamilyInstance adaptInst;
            try
            {
                adaptInst = doc.GetElement(existingElems[0]) as FamilyInstance;
            }
            catch
            {
                adaptInst = null;
            }
            if (adaptInst != null && AdaptiveComponentInstanceUtils.IsAdaptiveComponentInstance(adaptInst))
            {
                // Find the FamilySymbol
                FamilySymbol symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);

                if (symbol != null)
                {
                    using (Transaction t = new Transaction(doc, "Lyrebird Modify Objects"))
                    {
                        t.Start();
                        try
                        {
                            // Create the Schema for the instances to store the GH Component InstanceGUID and the path
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");
                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");
                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }

                            try
                            {
                                for (int i = 0; i < existingElems.Count; i++)
                                {
                                    RevitObject obj = existingObjects[i];

                                    FamilyInstance fi = doc.GetElement(existingElems[i]) as FamilyInstance;

                                    // Change the family and symbol if necessary
                                    if (fi.Symbol.Family.Name != symbol.Family.Name || fi.Symbol.Name != symbol.Name)
                                    {
                                        try
                                        {
                                            fi.Symbol = symbol;
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine(ex.Message);
                                        }
                                    }

                                    IList<ElementId> placePointIds = new List<ElementId>();
                                    placePointIds = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(fi);

                                    for (int ptNum = 0; ptNum < obj.AdaptivePoints.Count; ptNum++)
                                    {
                                        try
                                        {
                                            ReferencePoint rp = doc.GetElement(placePointIds[ptNum]) as ReferencePoint;
                                            XYZ pt = new XYZ(UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].X, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(obj.AdaptivePoints[ptNum].Z, lengthDUT));
                                            XYZ vector = pt.Subtract(rp.Position);
                                            ElementTransformUtils.MoveElement(doc, rp.Id, vector);
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine(ex.Message);
                                        }
                                    }

                                    // Assign the parameters
                                    SetParameters(fi, obj.Parameters, doc);
                                }

                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                        t.Commit();
                    }
                }

            }

            #endregion


            #region Curve based components
            if (ro.Curves != null && ro.Curves.Count > 0)
            {
                // Find the FamilySymbol
                FamilySymbol symbol = null;
                WallType wallType = null;
                FloorType floorType = null;
                RoofType roofType = null;
                GridType gridType = null;
                bool typeFound = false;

                FilteredElementCollector famCollector = new FilteredElementCollector(doc);

                if (ro.CategoryId == -2000011)
                {
                    famCollector.OfClass(typeof(WallType));
                    foreach (WallType wt in famCollector)
                    {
                        if (wt.Name == ro.TypeName)
                        {
                            wallType = wt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000032)
                {
                    famCollector.OfClass(typeof(FloorType));
                    foreach (FloorType ft in famCollector)
                    {
                        if (ft.Name == ro.TypeName)
                        {
                            floorType = ft;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000035)
                {
                    famCollector.OfClass(typeof(RoofType));
                    foreach (RoofType rt in famCollector)
                    {
                        if (rt.Name == ro.TypeName)
                        {
                            roofType = rt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else if (ro.CategoryId == -2000220)
                {
                    famCollector.OfClass(typeof(GridType));
                    foreach (GridType gt in famCollector)
                    {
                        if (gt.Name == ro.TypeName)
                        {
                            gridType = gt;
                            typeFound = true;
                            break;
                        }
                    }
                }
                else
                {
                    symbol = FindFamilySymbol(ro.FamilyName, ro.TypeName, doc);
                    if (symbol != null)
                        typeFound = true;
                }



                if (typeFound)
                {
                    
                    using (Transaction t = new Transaction(doc, "Lyrebird Modify Objects"))
                    {
                        t.Start();
                        try
                        {
                            // Create the Schema for the instances to store the GH Component InstanceGUID and the path
                            Schema instanceSchema = null;
                            try
                            {
                                instanceSchema = Schema.Lookup(instanceSchemaGUID);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                            }
                            if (instanceSchema == null)
                            {
                                SchemaBuilder sb = new SchemaBuilder(instanceSchemaGUID);
                                sb.SetWriteAccessLevel(AccessLevel.Vendor);
                                sb.SetReadAccessLevel(AccessLevel.Public);
                                sb.SetVendorId("LMNA");

                                // Create the field to store the data in the family
                                FieldBuilder guidFB = sb.AddSimpleField("InstanceID", typeof(string));
                                guidFB.SetDocumentation("Component instance GUID from Grasshopper");
                                // Create a filed to store the run number
                                FieldBuilder runIDFB = sb.AddSimpleField("RunID", typeof(int));
                                runIDFB.SetDocumentation("RunID for when multiple runs are created from the same data");
                                // Create a field to store the GH component nickname.
                                FieldBuilder nickNameFB = sb.AddSimpleField("NickName", typeof(string));
                                nickNameFB.SetDocumentation("Component NickName from Grasshopper");

                                sb.SetSchemaName("LMNAInstanceGUID");
                                instanceSchema = sb.Finish();
                            }
                            FamilyInstance fi = null;
                            Grid grid = null;
                            
                            try
                            {
                                bool supress = Properties.Settings.Default.suppressWarning;
                                bool supressedReplace = false;
                                bool supressedModify = true;
                                for (int i = 0; i < existingObjects.Count; i++)
                                {
                                    RevitObject obj = existingObjects[i];
                                    if (obj.CategoryId != -2000011 && obj.CategoryId != -2000032 && obj.CategoryId != -2000035 && obj.CategoryId != -2000220)
                                    {
                                        fi = doc.GetElement(existingElems[i]) as FamilyInstance;

                                        // Change the family and symbol if necessary
                                        if (fi.Symbol.Family.Name != symbol.Family.Name || fi.Symbol.Name != symbol.Name)
                                        {
                                            try
                                            {
                                                fi.Symbol = symbol;
                                            }
                                            catch (Exception ex)
                                            {
                                                Debug.WriteLine(ex.Message);
                                            }
                                        }
                                    }
                                    else if (obj.CategoryId == -2000220)
                                    {
                                        grid = doc.GetElement(existingElems[i]) as Grid;

                                        // Get the grid location and compare against the incoming curve
                                        Curve gridCrv = grid.Curve;
                                        LyrebirdCurve lbc = obj.Curves[0];
                                        try
                                        {
                                            Arc arc = gridCrv as Arc;
                                            if (arc != null && lbc.CurveType == "Arc")
                                            {
                                                // Test that the arcs are similar
                                                XYZ startPoint = arc.GetEndPoint(0);
                                                XYZ endPoint = arc.GetEndPoint(1);
                                                XYZ centerPoint = arc.Center;
                                                double rad = arc.Radius;

                                                XYZ lbcStartPt = new XYZ(lbc.ControlPoints[0].X, lbc.ControlPoints[0].Y, lbc.ControlPoints[0].Z);
                                                XYZ lbcEndPt = new XYZ(lbc.ControlPoints[1].X, lbc.ControlPoints[1].Y, lbc.ControlPoints[1].Z);
                                                XYZ lbcMidPt = new XYZ(lbc.ControlPoints[2].X, lbc.ControlPoints[2].Y, lbc.ControlPoints[2].Z);
                                                Arc lbcArc = Arc.Create(lbcStartPt, lbcEndPt, lbcMidPt);
                                                XYZ lbcCenterPt = lbcArc.Center;
                                                double lbcRad = lbcArc.Radius;

                                                if (centerPoint.DistanceTo(lbcCenterPt) < 0.001 && lbcRad == rad && startPoint.DistanceTo(lbcStartPt) < 0.001)
                                                {
                                                    // Do not create
                                                }
                                                else
                                                {
                                                    // Delete the grid and rebuild it with the new curve.
                                                }
                                            }
                                            else
                                            {
                                                // Probably need to rebuild the curve
                                            }
                                        }
                                        catch { }


                                        try
                                        {
                                            Line line = gridCrv as Line;
                                            if (line != null && lbc.CurveType == "Line")
                                            {
                                                // Test that the arcs are similar
                                                XYZ startPoint = line.GetEndPoint(0);
                                                XYZ endPoint = line.GetEndPoint(1);

                                                XYZ lbcStartPt = new XYZ(lbc.ControlPoints[0].X, lbc.ControlPoints[0].Y, lbc.ControlPoints[0].Z);
                                                XYZ lbcEndPt = new XYZ(lbc.ControlPoints[1].X, lbc.ControlPoints[1].Y, lbc.ControlPoints[1].Z);

                                                if (endPoint.DistanceTo(lbcEndPt) < 0.001 && startPoint.DistanceTo(lbcStartPt) < 0.001)
                                                {
                                                    // Do not create
                                                }
                                                else
                                                {
                                                    // Delete the grid and rebuild it with the new curve.
                                                }
                                            }
                                            else
                                            {
                                                // Probably need to rebuild the curve
                                            }
                                        }
                                        catch { }

                                        if (grid.GridType.Name != gridType.Name)
                                        {
                                            try
                                            {
                                                grid.GridType = gridType;
                                            }
                                            catch (Exception ex)
                                            {
                                                TaskDialog.Show("Error", ex.Message);
                                                Debug.WriteLine(ex.Message);
                                            }
                                        }
                                    }
                                    #region single line based family
                                    if (obj.Curves.Count == 1 && obj.Curves[0].CurveType != "Circle")
                                    {

                                        LyrebirdCurve lbc = obj.Curves[0];
                                        List<LyrebirdPoint> curvePoints = lbc.ControlPoints.OrderBy(p => p.Z).ToList();
                                        // linear
                                        // can be a wall or line based family.

                                        // Wall objects
                                        if (obj.CategoryId == -2000011)
                                        {
                                            // draw a wall
                                            Curve crv = null;
                                            if (lbc.CurveType == "Line")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                crv = Line.CreateBound(pt1, pt2);
                                            }
                                            else if (lbc.CurveType == "Arc")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                crv = Arc.Create(pt1, pt3, pt2);
                                            }

                                            if (crv != null)
                                            {

                                                // Find the level
                                                Level lvl = GetLevel(lbc.ControlPoints, doc);

                                                double offset = 0;
                                                if (Math.Abs(UnitUtils.ConvertToInternalUnits(curvePoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                                {
                                                    offset = lvl.Elevation - UnitUtils.ConvertToInternalUnits(curvePoints[0].Z, lengthDUT);
                                                }

                                                // Modify the wall
                                                Wall w = null;
                                                try
                                                {
                                                    w = doc.GetElement(existingElems[i]) as Wall;
                                                    LocationCurve lc = w.Location as LocationCurve;
                                                    lc.Curve = crv;

                                                    // Change the family and symbol if necessary
                                                    if (w.WallType.Name != wallType.Name)
                                                    {
                                                        try
                                                        {
                                                            w.WallType = wallType;
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            Debug.WriteLine(ex.Message);
                                                        }
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    TaskDialog.Show("ERROR", ex.Message);
                                                }


                                                // Assign the parameters
                                                SetParameters(w, obj.Parameters, doc);
                                            }
                                        }
                                        // See if it's a structural column
                                        else if (obj.CategoryId == -2001330)
                                        {
                                            if (symbol != null && lbc.CurveType == "Line")
                                            {
                                                Curve crv = null;
                                                XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                crv = Line.CreateBound(origin, pt2);

                                                // Find the level
                                                //Level lvl = GetLevel(lbc.ControlPoints, doc);

                                                // Create the column
                                                //fi = doc.Create.NewFamilyInstance(origin, symbol, lvl, Autodesk.Revit.DB.Structure.StructuralType.Column);

                                                // Change it to a slanted column
                                                Parameter slantParam = fi.get_Parameter(BuiltInParameter.SLANTED_COLUMN_TYPE_PARAM);

                                                // SlantedOrVerticalColumnType has 3 options, CT_Vertical (0), CT_Angle (1), or CT_EndPoint (2)
                                                // CT_EndPoint is what we want for a line based column.
                                                slantParam.Set(2);

                                                // Set the location curve of the column to the line
                                                LocationCurve lc = fi.Location as LocationCurve;
                                                if (lc != null)
                                                {
                                                    lc.Curve = crv;
                                                }

                                                // Change the family and symbol if necessary
                                                if (fi.Symbol.Name != symbol.Name)
                                                {
                                                    try
                                                    {
                                                        fi.Symbol = symbol;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }

                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);
                                            }
                                        }

                                        else if (obj.CategoryId == -2000220)
                                        {
                                            // draw a grid
                                            Curve crv = null;
                                            if (lbc.CurveType == "Line")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                crv = Line.CreateBound(pt1, pt2);
                                            }
                                            else if (lbc.CurveType == "Arc")
                                            {
                                                XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                crv = Arc.Create(pt1, pt3, pt2);
                                            }

                                            if (crv != null && grid != null)
                                            {
                                                // Determine if it's possible to edit the grid curve or if it needs to be deleted/replaced.

                                                // Assign the parameters
                                                SetParameters(grid, obj.Parameters, doc);
                                            }
                                        }

                                        // Otherwise create a family it using the line
                                        else
                                        {
                                            if (symbol != null)
                                            {
                                                Curve crv = null;

                                                if (lbc.CurveType == "Line")
                                                {
                                                    XYZ origin = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                    XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                    crv = Line.CreateBound(origin, pt2);
                                                }
                                                else if (lbc.CurveType == "Arc")
                                                {
                                                    XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                    XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                    XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                    crv = Arc.Create(pt1, pt3, pt2);
                                                }

                                                // Change the family and symbol if necessary
                                                if (fi.Symbol.Name != symbol.Name)
                                                {
                                                    try
                                                    {
                                                        fi.Symbol = symbol;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }

                                                try
                                                {
                                                    LocationCurve lc = fi.Location as LocationCurve;
                                                    lc.Curve = crv;
                                                }
                                                catch (Exception ex)
                                                {
                                                    Debug.WriteLine(ex.Message);
                                                }
                                                // Assign the parameters
                                                SetParameters(fi, obj.Parameters, doc);
                                            }
                                        }
                                    }
                                    #endregion

                                    #region Closed Curve Family
                                    else
                                    {
                                        bool replace = false;
                                        if (supress)
                                        {
                                            if (supressedReplace)
                                            {
                                                replace = true;
                                            }
                                            else
                                            {
                                                replace = false;
                                            }
                                        }
                                        if (profileWarning && !supress)
                                        {
                                            TaskDialog warningDlg = new TaskDialog("Warning")
                                            {
                                                MainInstruction = "Profile based Elements warning",
                                                MainContent =
                                                  "Elements that require updates to a profile sketch may not be updated if the number of curves in the sketch differs from the incoming curves." +
                                                  "  In such cases the element and will be deleted and replaced with new elements." +
                                                  "  Doing so will cause the loss of any elements hosted to the original instance. How would you like to proceed"
                                            };

                                            warningDlg.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Replace the existing elements, understanding hosted elements may be lost");
                                            warningDlg.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Only updated parameter information and not profile or location information");
                                            warningDlg.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "Cancel");
                                            //warningDlg.VerificationText = "Supress similar warnings";

                                            TaskDialogResult result = warningDlg.Show();
                                            if (result == TaskDialogResult.CommandLink1)
                                            {
                                                replace = true;
                                                supressedReplace = true;
                                                supressedModify = true;
                                                //supress = warningDlg.WasVerificationChecked();
                                            }
                                            if (result == TaskDialogResult.CommandLink2)
                                            {
                                                supressedReplace = false;
                                                supressedModify = true;
                                                //supress = warningDlg.WasVerificationChecked();
                                            }
                                            if (result == TaskDialogResult.CommandLink3)
                                            {
                                                supressedReplace = false;
                                                supressedModify = false;
                                                //supress = warningDlg.WasVerificationChecked();
                                            }
                                        }
                                        // A list of curves.  These should equate a closed planar curve from GH.
                                        // Determine category and create based on that.
                                        #region walls
                                        if (obj.CategoryId == -2000011)
                                        {
                                            // Create line based wall
                                            // Find the level
                                            Level lvl = null;
                                            double offset = 0;
                                            List<LyrebirdPoint> allPoints = new List<LyrebirdPoint>();
                                            foreach (LyrebirdCurve lc in obj.Curves)
                                            {
                                                foreach (LyrebirdPoint lp in lc.ControlPoints)
                                                {
                                                    allPoints.Add(lp);
                                                }
                                            }
                                            allPoints.Sort((x, y) => x.Z.CompareTo(y.Z));

                                            lvl = GetLevel(allPoints, doc);

                                            if (Math.Abs(allPoints[0].Z - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = allPoints[0].Z - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            List<Curve> crvArray = new List<Curve>();
                                            try
                                            {
                                                foreach (LyrebirdCurve lbc in obj.Curves)
                                                {
                                                    if (lbc.CurveType == "Circle")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                        XYZ pt4 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[3].Z, lengthDUT));
                                                        XYZ pt5 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[4].Z, lengthDUT));
                                                        Arc arc1 = Arc.Create(pt1, pt3, pt2);
                                                        Arc arc2 = Arc.Create(pt3, pt5, pt4);
                                                        crvArray.Add(arc1);
                                                        crvArray.Add(arc2);
                                                    }
                                                    else if (lbc.CurveType == "Arc")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        XYZ pt3 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[2].Z, lengthDUT));
                                                        Arc arc = Arc.Create(pt1, pt3, pt2);
                                                        crvArray.Add(arc);
                                                    }
                                                    else if (lbc.CurveType == "Line")
                                                    {
                                                        XYZ pt1 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[0].Z, lengthDUT));
                                                        XYZ pt2 = new XYZ(UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].X, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lbc.ControlPoints[1].Z, lengthDUT));
                                                        Line line = Line.CreateBound(pt1, pt2);
                                                        crvArray.Add(line);
                                                    }
                                                    else if (lbc.CurveType == "Spline")
                                                    {
                                                        List<XYZ> controlPoints = new List<XYZ>();
                                                        List<double> weights = lbc.Weights;
                                                        List<double> knots = lbc.Knots;

                                                        foreach (LyrebirdPoint lp in lbc.ControlPoints)
                                                        {
                                                            XYZ pt = new XYZ(UnitUtils.ConvertToInternalUnits(lp.X, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Y, lengthDUT), UnitUtils.ConvertToInternalUnits(lp.Z, lengthDUT));
                                                            controlPoints.Add(pt);
                                                        }

                                                        NurbSpline spline;
                                                        if (lbc.Degree < 3)
                                                            spline = NurbSpline.Create(controlPoints, weights);
                                                        else
                                                            spline = NurbSpline.Create(controlPoints, weights, knots, lbc.Degree, false, true);

                                                        crvArray.Add(spline);
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                TaskDialog.Show("ERROR", ex.Message);
                                            }

                                            // Create the wall
                                            Wall w = null;
                                            if (replace)
                                            {
                                                Wall origWall = doc.GetElement(existingElems[i]) as Wall;

                                                // Find the model curves for the original wall
                                                ICollection<ElementId> ids;
                                                using (SubTransaction st = new SubTransaction(doc))
                                                {
                                                    st.Start();
                                                    ids = doc.Delete(origWall.Id);
                                                    st.RollBack();
                                                }

                                                List<ModelCurve> mLines = new List<ModelCurve>();
                                                foreach (ElementId id in ids)
                                                {
                                                    Element e = doc.GetElement(id);
                                                    if (e is ModelCurve)
                                                    {
                                                        mLines.Add(e as ModelCurve);
                                                    }
                                                }

                                                // Walls don't appear to be updatable like floors and roofs
                                                //if (mLines.Count != crvArray.Count)
                                                //{

                                                w = Wall.Create(doc, crvArray, wallType.Id, lvl.Id, false);

                                                foreach (Parameter p in origWall.Parameters)
                                                {
                                                    try
                                                    {
                                                        Parameter newParam = w.LookupParameter(p.Definition.Name);
                                                        if (newParam != null)
                                                        {
                                                            switch (newParam.StorageType)
                                                            {
                                                                case StorageType.Double:
                                                                    newParam.Set(p.AsDouble());
                                                                    break;
                                                                case StorageType.ElementId:
                                                                    newParam.Set(p.AsElementId());
                                                                    break;
                                                                case StorageType.Integer:
                                                                    newParam.Set(p.AsInteger());
                                                                    break;
                                                                case StorageType.String:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                                default:
                                                                    newParam.Set(p.AsString());
                                                                    break;
                                                            }
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        //TaskDialog.Show("Errorsz", ex.Message);
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }

                                                if (Math.Abs(offset - 0) > double.Epsilon)
                                                {
                                                    Parameter p = w.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
                                                    p.Set(offset);
                                                }
                                                doc.Delete(origWall.Id);

                                                // Assign the parameters
                                                SetParameters(w, obj.Parameters, doc);

                                                // Assign the GH InstanceGuid
                                                AssignGuid(w, uniqueId, instanceSchema, runId, nickName);
                                            }
                                            else if (supressedModify) // Just update the parameters and don't change the wall
                                            {
                                                w = doc.GetElement(existingElems[i]) as Wall;

                                                // Change the family and symbol if necessary
                                                if (w.WallType.Name != wallType.Name)
                                                {
                                                    try
                                                    {
                                                        w.WallType = wallType;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }

                                                // Assign the parameters
                                                SetParameters(w, obj.Parameters, doc);
                                            }
                                        }
                                        #endregion

                                        #region floors
                                        else if (obj.CategoryId == -2000032)
                                        {
                                            // Create a profile based floor
                                            // Find the level
                                            Level lvl = GetLevel(obj.Curves[0].ControlPoints, doc);

                                            double offset = 0;
                                            if (Math.Abs(UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            CurveArray crvArray = GetCurveArray(obj.Curves);

                                            // Create the floor
                                            Floor flr = null;
                                            if (replace)
                                            {
                                                Floor origFloor = doc.GetElement(existingElems[i]) as Floor;

                                                // Find the model curves for the original wall
                                                ICollection<ElementId> ids;
                                                using (SubTransaction st = new SubTransaction(doc))
                                                {
                                                    st.Start();

                                                    ids = doc.Delete(origFloor.Id);
                                                    st.RollBack();
                                                }

                                                // Get only the modelcurves
                                                List<ModelCurve> mLines = new List<ModelCurve>();
                                                foreach (ElementId id in ids)
                                                {
                                                    Element e = doc.GetElement(id);
                                                    if (e is ModelCurve)
                                                    {
                                                        mLines.Add(e as ModelCurve);
                                                    }
                                                }

                                                // Floors have an extra modelcurve for the SpanDirection.  Remove the last Item to get rid of it.
                                                mLines.RemoveAt(mLines.Count - 1);
                                                if (mLines.Count != crvArray.Size) // The sketch is different from the incoming curves so floor is recreated
                                                {
                                                    if (obj.CurveIds != null && obj.CurveIds.Count > 0)
                                                    {
                                                        // get the main profile
                                                        int crvCount = obj.CurveIds[0];
                                                        List<LyrebirdCurve> primaryCurves = obj.Curves.GetRange(0, crvCount);
                                                        crvArray = GetCurveArray(primaryCurves);
                                                        flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);
                                                    }
                                                    else
                                                    {
                                                        flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);
                                                    }
                                                    foreach (Parameter p in origFloor.Parameters)
                                                    {
                                                        try
                                                        {
                                                            Parameter newParam = flr.LookupParameter(p.Definition.Name);
                                                            if (newParam != null)
                                                            {
                                                                switch (newParam.StorageType)
                                                                {
                                                                    case StorageType.Double:
                                                                        newParam.Set(p.AsDouble());
                                                                        break;
                                                                    case StorageType.ElementId:
                                                                        newParam.Set(p.AsElementId());
                                                                        break;
                                                                    case StorageType.Integer:
                                                                        newParam.Set(p.AsInteger());
                                                                        break;
                                                                    case StorageType.String:
                                                                        newParam.Set(p.AsString());
                                                                        break;
                                                                    default:
                                                                        newParam.Set(p.AsString());
                                                                        break;
                                                                }
                                                            }
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            Debug.WriteLine(ex.Message);
                                                        }
                                                    }

                                                    if (Math.Abs(offset - 0) > double.Epsilon)
                                                    {
                                                        Parameter p = flr.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM);
                                                        p.Set(offset);
                                                    }
                                                    doc.Delete(origFloor.Id);

                                                    // Assign the parameters
                                                    SetParameters(flr, obj.Parameters, doc);

                                                    // Assign the GH InstanceGuid
                                                    AssignGuid(flr, uniqueId, instanceSchema, runId, nickName);
                                                }
                                                else // The curves coming in should match the floor sketch.  Let's modify the floor's locationcurves to edit it's location/shape
                                                {
                                                    try
                                                    {
                                                        int crvCount = 0;
                                                        foreach (ModelCurve l in mLines)
                                                        {
                                                            LocationCurve lc = l.Location as LocationCurve;
                                                            lc.Curve = crvArray.get_Item(crvCount);
                                                            crvCount++;
                                                        }

                                                        // Change the family and symbol if necessary
                                                        if (origFloor.FloorType.Name != floorType.Name)
                                                        {
                                                            try
                                                            {
                                                                origFloor.FloorType = floorType;
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                Debug.WriteLine(ex.Message);
                                                            }
                                                        }

                                                        // Set the incoming parameters
                                                        SetParameters(origFloor, obj.Parameters, doc);
                                                    }
                                                    catch // There was an error in trying to recreate it.  Just delete the original and recreate the thing.
                                                    {
                                                        flr = doc.Create.NewFloor(crvArray, floorType, lvl, false);

                                                        // Assign the parameters in the new floor to match the original floor object.
                                                        foreach (Parameter p in origFloor.Parameters)
                                                        {
                                                            try
                                                            {
                                                                Parameter newParam = flr.LookupParameter(p.Definition.Name);
                                                                if (newParam != null)
                                                                {
                                                                    switch (newParam.StorageType)
                                                                    {
                                                                        case StorageType.Double:
                                                                            newParam.Set(p.AsDouble());
                                                                            break;
                                                                        case StorageType.ElementId:
                                                                            newParam.Set(p.AsElementId());
                                                                            break;
                                                                        case StorageType.Integer:
                                                                            newParam.Set(p.AsInteger());
                                                                            break;
                                                                        case StorageType.String:
                                                                            newParam.Set(p.AsString());
                                                                            break;
                                                                        default:
                                                                            newParam.Set(p.AsString());
                                                                            break;
                                                                    }
                                                                }
                                                            }
                                                            catch (Exception exception)
                                                            {
                                                                Debug.WriteLine(exception.Message);
                                                            }
                                                        }

                                                        if (Math.Abs(offset - 0) > double.Epsilon)
                                                        {
                                                            Parameter p = flr.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM);
                                                            p.Set(offset);
                                                        }

                                                        doc.Delete(origFloor.Id);

                                                        // Set the incoming parameters
                                                        SetParameters(flr, obj.Parameters, doc);
                                                        // Assign the GH InstanceGuid
                                                        AssignGuid(flr, uniqueId, instanceSchema, runId, nickName);
                                                    }
                                                }
                                            }
                                            else if (supressedModify) // Just modify the floor and don't risk replacing it.
                                            {
                                                flr = doc.GetElement(existingElems[i]) as Floor;

                                                // Change the family and symbol if necessary
                                                if (flr.FloorType.Name != floorType.Name)
                                                {
                                                    try
                                                    {
                                                        flr.FloorType = floorType;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                // Assign the parameters
                                                SetParameters(flr, obj.Parameters, doc);
                                            }
                                        }
                                        #endregion
                                        else if (obj.CategoryId == -2000035)
                                        {
                                            // Create a RoofExtrusion
                                            // Find the level
                                            Level lvl = GetLevel(obj.Curves[0].ControlPoints, doc);

                                            double offset = 0;
                                            if (Math.Abs(UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation) > double.Epsilon)
                                            {
                                                offset = UnitUtils.ConvertToInternalUnits(obj.Curves[0].ControlPoints[0].Z, lengthDUT) - lvl.Elevation;
                                            }

                                            // Generate the curvearray from the incoming curves
                                            CurveArray crvArray = GetCurveArray(obj.Curves);

                                            // Create the roof
                                            FootPrintRoof roof = null;
                                            ModelCurveArray roofProfile = new ModelCurveArray();

                                            if (replace)  // Try to modify or create a new roof.
                                            {
                                                FootPrintRoof origRoof = doc.GetElement(existingElems[i]) as FootPrintRoof;

                                                // Find the model curves for the original wall
                                                ICollection<ElementId> ids;
                                                using (SubTransaction st = new SubTransaction(doc))
                                                {
                                                    st.Start();
                                                    ids = doc.Delete(origRoof.Id);
                                                    st.RollBack();
                                                }

                                                // Get the sketch curves for the roof object.
                                                List<ModelCurve> mLines = new List<ModelCurve>();
                                                foreach (ElementId id in ids)
                                                {
                                                    Element e = doc.GetElement(id);
                                                    if (e is ModelCurve)
                                                    {
                                                        mLines.Add(e as ModelCurve);
                                                    }
                                                }

                                                if (mLines.Count != crvArray.Size) // Sketch curves qty doesn't match up with the incoming cuves.  Just recreate the roof.
                                                {
                                                    roof = doc.Create.NewFootPrintRoof(crvArray, lvl, roofType, out roofProfile);

                                                    // Match parameters from the original roof to it's new iteration.
                                                    foreach (Parameter p in origRoof.Parameters)
                                                    {
                                                        try
                                                        {
                                                            Parameter newParam = roof.LookupParameter(p.Definition.Name);
                                                            if (newParam != null)
                                                            {
                                                                switch (newParam.StorageType)
                                                                {
                                                                    case StorageType.Double:
                                                                        newParam.Set(p.AsDouble());
                                                                        break;
                                                                    case StorageType.ElementId:
                                                                        newParam.Set(p.AsElementId());
                                                                        break;
                                                                    case StorageType.Integer:
                                                                        newParam.Set(p.AsInteger());
                                                                        break;
                                                                    case StorageType.String:
                                                                        newParam.Set(p.AsString());
                                                                        break;
                                                                    default:
                                                                        newParam.Set(p.AsString());
                                                                        break;
                                                                }
                                                            }
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            Debug.WriteLine(ex.Message);
                                                        }
                                                    }

                                                    if (Math.Abs(offset - 0) > double.Epsilon)
                                                    {
                                                        Parameter p = roof.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM);
                                                        p.Set(offset);
                                                    }

                                                    doc.Delete(origRoof.Id);

                                                    // Set the new parameters
                                                    SetParameters(roof, obj.Parameters, doc);

                                                    // Assign the GH InstanceGuid
                                                    AssignGuid(roof, uniqueId, instanceSchema, runId, nickName);
                                                }
                                                else // The curves qty lines up, lets try to modify the roof sketch so we don't have to replace it.
                                                {
                                                    if (obj.CurveIds != null && obj.CurveIds.Count > 0)
                                                    {
                                                        // Just recreate the roof
                                                        roof = doc.Create.NewFootPrintRoof(crvArray, lvl, roofType, out roofProfile);

                                                        // Match parameters from the original roof to it's new iteration.
                                                        foreach (Parameter p in origRoof.Parameters)
                                                        {
                                                            try
                                                            {
                                                                Parameter newParam = roof.LookupParameter(p.Definition.Name);
                                                                if (newParam != null)
                                                                {
                                                                    switch (newParam.StorageType)
                                                                    {
                                                                        case StorageType.Double:
                                                                            newParam.Set(p.AsDouble());
                                                                            break;
                                                                        case StorageType.ElementId:
                                                                            newParam.Set(p.AsElementId());
                                                                            break;
                                                                        case StorageType.Integer:
                                                                            newParam.Set(p.AsInteger());
                                                                            break;
                                                                        case StorageType.String:
                                                                            newParam.Set(p.AsString());
                                                                            break;
                                                                        default:
                                                                            newParam.Set(p.AsString());
                                                                            break;
                                                                    }
                                                                }
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                Debug.WriteLine(ex.Message);
                                                            }
                                                        }

                                                        if (Math.Abs(offset - 0) > double.Epsilon)
                                                        {
                                                            Parameter p = roof.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM);
                                                            p.Set(offset);
                                                        }

                                                        // Set the parameters from the incoming data
                                                        SetParameters(roof, obj.Parameters, doc);

                                                        // Assign the GH InstanceGuid
                                                        AssignGuid(roof, uniqueId, instanceSchema, runId, nickName);

                                                        doc.Delete(origRoof.Id);
                                                    }
                                                    else
                                                    {
                                                        try
                                                        {
                                                            int crvCount = 0;
                                                            foreach (ModelCurve l in mLines)
                                                            {
                                                                LocationCurve lc = l.Location as LocationCurve;
                                                                lc.Curve = crvArray.get_Item(crvCount);
                                                                crvCount++;
                                                            }

                                                            // Change the family and symbol if necessary
                                                            if (origRoof.RoofType.Name != roofType.Name)
                                                            {
                                                                try
                                                                {
                                                                    origRoof.RoofType = roofType;
                                                                }
                                                                catch (Exception ex)
                                                                {
                                                                    Debug.WriteLine(ex.Message);
                                                                }
                                                            }

                                                            SetParameters(origRoof, obj.Parameters, doc);
                                                        }
                                                        catch // Modificaiton failed, lets just create a new roof.
                                                        {
                                                            roof = doc.Create.NewFootPrintRoof(crvArray, lvl, roofType, out roofProfile);

                                                            // Match parameters from the original roof to it's new iteration.
                                                            foreach (Parameter p in origRoof.Parameters)
                                                            {
                                                                try
                                                                {
                                                                    Parameter newParam = roof.LookupParameter(p.Definition.Name);
                                                                    if (newParam != null)
                                                                    {
                                                                        switch (newParam.StorageType)
                                                                        {
                                                                            case StorageType.Double:
                                                                                newParam.Set(p.AsDouble());
                                                                                break;
                                                                            case StorageType.ElementId:
                                                                                newParam.Set(p.AsElementId());
                                                                                break;
                                                                            case StorageType.Integer:
                                                                                newParam.Set(p.AsInteger());
                                                                                break;
                                                                            case StorageType.String:
                                                                                newParam.Set(p.AsString());
                                                                                break;
                                                                            default:
                                                                                newParam.Set(p.AsString());
                                                                                break;
                                                                        }
                                                                    }
                                                                }
                                                                catch (Exception ex)
                                                                {
                                                                    Debug.WriteLine(ex.Message);
                                                                }
                                                            }

                                                            if (Math.Abs(offset - 0) > double.Epsilon)
                                                            {
                                                                Parameter p = roof.get_Parameter(BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM);
                                                                p.Set(offset);
                                                            }

                                                            // Set the parameters from the incoming data
                                                            SetParameters(roof, obj.Parameters, doc);

                                                            // Assign the GH InstanceGuid
                                                            AssignGuid(roof, uniqueId, instanceSchema, runId, nickName);

                                                            doc.Delete(origRoof.Id);
                                                        }
                                                    }
                                                }
                                            }
                                            else if (supressedModify) // Only update the parameters
                                            {
                                                roof = doc.GetElement(existingElems[i]) as FootPrintRoof;

                                                // Change the family and symbol if necessary
                                                if (roof.RoofType.Name != roofType.Name)
                                                {
                                                    try
                                                    {
                                                        roof.RoofType = roofType;
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Debug.WriteLine(ex.Message);
                                                    }
                                                }
                                                // Assign the parameters
                                                SetParameters(roof, obj.Parameters, doc);
                                            }
                                        }
                                    }
                                    #endregion
                                }
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Error", ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                        t.Commit();
                    }
                }


            }
            #endregion

            //return succeeded;
        }
        /// <summary>
        /// 获取当前模型指定视图内的所有最外层的墙体
        /// Get all the outermost walls in the
        /// specified view of the current model
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="view">视图,默认是当前激活的视图
        /// View, default is currently active view</param>
        public static List <ElementId> GetOutermostWalls(
            Document doc,
            View view = null)
        {
            double offset = Util.MmToFoot(1000);

            if (view == null)
            {
                view = doc.ActiveView;
            }

            #region Obsolete code using wall location line instad of bounding box
            //获取顶点 最小x,最大y ; 最大x,最小y

#if BEFORE_USING_BOUNDING_BOX
            List <Wall> wallList = new FilteredElementCollector(doc)
                                   .OfClass(typeof(Wall))
                                   .Cast <Wall>()
                                   .ToList();

            double maxX = -1D;
            double minX = -1D;
            double maxY = -1D;
            double minY = -1D;

            wallList.ForEach((wall) =>
            {
                Curve curve = (wall.Location as LocationCurve).Curve;
                XYZ xyz1    = curve.GetEndPoint(0);
                XYZ xyz2    = curve.GetEndPoint(1);

                double _minX = Math.Min(xyz1.X, xyz2.X);
                double _maxX = Math.Max(xyz1.X, xyz2.X);
                double _minY = Math.Min(xyz1.Y, xyz2.Y);
                double _maxY = Math.Max(xyz1.Y, xyz2.Y);

                if (curve.IsCyclic)
                {
                    Arc arc        = curve as Arc;
                    double _radius = arc.Radius;
                    //粗略对x和y 加/减
                    _maxX += _radius;
                    _minX -= _radius;
                    _maxY += _radius;
                    _minY += _radius;
                }

                if (minX == -1)
                {
                    minX = _minX;
                }
                if (maxX == -1)
                {
                    maxX = _maxX;
                }
                if (maxY == -1)
                {
                    maxY = _maxY;
                }
                if (minY == -1)
                {
                    minY = _minY;
                }

                if (_minX < minX)
                {
                    minX = _minX;
                }
                if (_maxX > maxX)
                {
                    maxX = _maxX;
                }
                if (_maxY > maxY)
                {
                    maxY = _maxY;
                }
                if (_minY < minY)
                {
                    minY = _minY;
                }
            });
            double     minX   = bb.Min.X - offset;
            double     maxX   = bb.Max.X + offset;
            double     minY   = bb.Min.Y - offset;
            double     maxY   = bb.Max.Y + offset;
            CurveArray curves = new CurveArray();
            Line       line1  = Line.CreateBound(new XYZ(minX, maxY, 0), new XYZ(maxX, maxY, 0));
            Line       line2  = Line.CreateBound(new XYZ(maxX, maxY, 0), new XYZ(maxX, minY, 0));
            Line       line3  = Line.CreateBound(new XYZ(maxX, minY, 0), new XYZ(minX, minY, 0));
            Line       line4  = Line.CreateBound(new XYZ(minX, minY, 0), new XYZ(minX, maxY, 0));
            curves.Append(line1);
            curves.Append(line2);
            curves.Append(line3);
            curves.Append(line4);
#endif // BEFORE_USING_BOUNDING_BOX
            #endregion // Obsolete code using wall location line instad of bounding box

            BoundingBoxXYZ bb = GetBoundingBoxAroundAllWalls(
                doc, view);

            XYZ voffset = offset * (XYZ.BasisX + XYZ.BasisY);
            bb.Min -= voffset;
            bb.Max += voffset;

            XYZ[] bottom_corners = Util.GetBottomCorners(
                bb, 0);

            CurveArray curves = new CurveArray();
            for (int i = 0; i < 4; ++i)
            {
                int j = i < 3 ? i + 1 : 0;
                curves.Append(Line.CreateBound(
                                  bottom_corners[i], bottom_corners[j]));
            }

            using (TransactionGroup group
                       = new TransactionGroup(doc))
            {
                Room newRoom = null;

                group.Start("Find Outermost Walls");

                using (Transaction transaction
                           = new Transaction(doc))
                {
                    transaction.Start(
                        "Create New Room Boundary Lines");

                    SketchPlane sketchPlane = SketchPlane.Create(
                        doc, view.GenLevel.Id);

                    ModelCurveArray modelCaRoomBoundaryLines
                        = doc.Create.NewRoomBoundaryLines(
                              sketchPlane, curves, view);

                    // 创建房间的坐标点 -- Create room coordinates

                    double d     = Util.MmToFoot(600);
                    UV     point = new UV(bb.Min.X + d, bb.Min.Y + d);

                    // 根据选中点,创建房间 当前视图的楼层 doc.ActiveView.GenLevel
                    // Create room at selected point on the current view level

                    newRoom = doc.Create.NewRoom(view.GenLevel, point);

                    if (newRoom == null)
                    {
                        string msg = "创建房间失败。";
                        TaskDialog.Show("xx", msg);
                        transaction.RollBack();
                        return(null);
                    }

                    RoomTag tag = doc.Create.NewRoomTag(
                        new LinkElementId(newRoom.Id),
                        point, view.Id);

                    transaction.Commit();
                }

                //获取房间的墙体 -- Get the room walls

                List <ElementId> ids
                    = RetrieveWallsGeneratingRoomBoundaries(
                          doc, newRoom);

                group.RollBack(); // 撤销

                return(ids);
            }
        }
        void f( Document doc )
        {
            // Before invoking this sample, select some walls
              // to add a roof over. Make sure there is a level
              // named "Roof" in the document.

              Level level
            = new FilteredElementCollector( doc )
              .OfClass( typeof( Level ) )
              .Where<Element>( e =>
            !string.IsNullOrEmpty( e.Name )
            && e.Name.Equals( "Roof" ) )
              .FirstOrDefault<Element>() as Level;

              RoofType roofType
            = new FilteredElementCollector( doc )
              .OfClass( typeof( RoofType ) )
              .FirstOrDefault<Element>() as RoofType;

              // Get the handle of the application
              Application application = doc.Application;

              // Define the footprint for the roof based on user selection
              CurveArray footprint = application.Create
            .NewCurveArray();

              UIDocument uidoc = new UIDocument( doc );

              ICollection<ElementId> selectedIds
            = uidoc.Selection.GetElementIds();

              if( selectedIds.Count != 0 )
              {
            foreach( ElementId id in selectedIds )
            {
              Element element = doc.GetElement( id );
              Wall wall = element as Wall;
              if( wall != null )
              {
            LocationCurve wallCurve = wall.Location as LocationCurve;
            footprint.Append( wallCurve.Curve );
            continue;
              }

              ModelCurve modelCurve = element as ModelCurve;
              if( modelCurve != null )
              {
            footprint.Append( modelCurve.GeometryCurve );
              }
            }
              }
              else
              {
            throw new Exception(
              "Please select a curve loop, wall loop or "
              + "combination of walls and curves to "
              + "create a footprint roof." );
              }

              ModelCurveArray footPrintToModelCurveMapping
            = new ModelCurveArray();

              FootPrintRoof footprintRoof
            = doc.Create.NewFootPrintRoof(
              footprint, level, roofType,
              out footPrintToModelCurveMapping );

              ModelCurveArrayIterator iterator
            = footPrintToModelCurveMapping.ForwardIterator();

              iterator.Reset();
              while( iterator.MoveNext() )
              {
            ModelCurve modelCurve = iterator.Current as ModelCurve;
            footprintRoof.set_DefinesSlope( modelCurve, true );
            footprintRoof.set_SlopeAngle( modelCurve, 0.5 );
              }
        }
        /// <summary>
        /// Add a roof over the rectangular profile of the walls we created earlier.
        /// </summary>
        public static void AddRoof(Document rvtDoc, List <Wall> walls)
        {
            // Hard coding the roof type we will use.
            // e.g., "Basic Roof: Generic - 400mm"
            const string roofFamilyName        = "Basic Roof";
            const string roofTypeName          = Util.Constant.RoofTypeName;
            const string roofFamilyAndTypeName = roofFamilyName + ": " + roofTypeName;

            // Find the roof type
            RoofType roofType =
                ElementFiltering.FindFamilyType(
                    rvtDoc, typeof(RoofType), roofFamilyName, roofTypeName, null
                    ) as RoofType;

            if (roofType == null)
            {
                TaskDialog.Show(
                    "Add roof", "Cannot find (" + roofFamilyAndTypeName +
                    "). Maybe you use a different template? Try with DefaultMetric.rte.");
            }

            // Wall thickness to adjust the footprint of the walls
            // to the outer most lines.
            // Note: this may not be the best way.
            // but we will live with this for this exercise.
            //Dim wallThickness As Double = _
            //walls(0).WallType.CompoundStructure.Layers.Item(0).Thickness() ' 2011
            double wallThickness = walls[0].WallType.GetCompoundStructure().GetLayers()[0].Width;
            // 2012
            double     dt  = wallThickness / 2.0;
            List <XYZ> dts = new List <XYZ>(5);

            dts.Add(new XYZ(-dt, -dt, 0.0));
            dts.Add(new XYZ(dt, -dt, 0.0));
            dts.Add(new XYZ(dt, dt, 0.0));
            dts.Add(new XYZ(-dt, dt, 0.0));
            dts.Add(dts[0]);

            // Set the profile from four walls
            CurveArray footPrint = new CurveArray();

            for (int i = 0; i <= 3; i++)
            {
                LocationCurve locCurve = (LocationCurve)walls[i].Location;
                XYZ           pt1      = locCurve.Curve.GetEndPoint(0) + dts[i];
                XYZ           pt2      = locCurve.Curve.GetEndPoint(1) + dts[i + 1];
                Line          line     = Line.CreateBound(pt1, pt2);
                footPrint.Append(line);
            }

            // Get the level2 from the wall.

            ElementId idLevel2 = walls[0].get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId();

            //Level level2 = (Level)_doc.get_Element(idLevel2); // 2012
            Level level2 = rvtDoc.GetElement(idLevel2) as Level; // since 2013

            // Footprint to model curve mapping.

            ModelCurveArray mapping = new ModelCurveArray();

            // Create a roof.

            FootPrintRoof aRoof = rvtDoc.Create.NewFootPrintRoof(
                footPrint, level2, roofType, out mapping);

            // Set the slope.

            foreach (ModelCurve modelCurve in mapping)
            {
                aRoof.set_DefinesSlope(modelCurve, true);
                aRoof.set_SlopeAngle(modelCurve, 0.5);
            }

            // Performed automatically by transaction commit.
            //rvtDoc.Regenerate();
            //rvtDoc.AutoJoinElements();
        }
Example #39
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            try
            {
                WaitCursor    waitCursor = new WaitCursor();
                UIApplication app        = commandData.Application;
                Document      doc        = app.ActiveUIDocument.Document;
                Autodesk.Revit.Creation.Application createApp = app.Application.Create;
                Autodesk.Revit.Creation.Document    createDoc = doc.Create;

                using (Transaction t = new Transaction(doc))
                {
                    t.Start("Create Little House");

                    // Determine the four corners of the rectangular house:

                    double width = 7 * LabConstants.MeterToFeet;
                    double depth = 4 * LabConstants.MeterToFeet;

                    List <XYZ> corners = new List <XYZ>(4);

                    corners.Add(XYZ.Zero);
                    corners.Add(new XYZ(width, 0, 0));
                    corners.Add(new XYZ(width, depth, 0));
                    corners.Add(new XYZ(0, depth, 0));

                    #region Test creating two levels
#if CREATE_TWO_LEVELS
                    Level          levelBottom = null;
                    Level          levelMiddle = null;
                    Level          levelTop    = null;
                    List <Element> levels      = new List <Element>();

                    Filter filterType
                        = createApp.Filter.NewTypeFilter(
                              typeof(Level));

                    doc.get_Elements(filterType, levels);
                    foreach (Element e in levels)
                    {
                        if (null == levelBottom)
                        {
                            levelBottom = e as Level;
                        }
                        else if (null == levelMiddle)
                        {
                            levelMiddle = e as Level;
                        }
                        else if (null == levelTop)
                        {
                            levelTop = e as Level;
                        }
                        else
                        {
                            break;
                        }
                    }

                    BuiltInParameter topLevelParam
                        = BuiltInParameter.WALL_HEIGHT_TYPE;

                    Line      line;
                    Wall      wall;
                    Parameter param;

                    ElementId   topId = levelMiddle.Id;
                    List <Wall> walls = new List <Wall>(8);
                    for (int i = 0; i < 4; ++i)
                    {
                        line = createApp.NewLineBound(
                            corners[i], corners[3 == i ? 0 : i + 1]);

                        wall = createDoc.NewWall(
                            line, levelBottom, false);

                        param = wall.get_Parameter(topLevelParam);
                        param.Set(ref topId);
                        walls.Add(wall);
                    }

                    topId = levelTop.Id;
                    for (int i = 0; i < 4; ++i)
                    {
                        line = createApp.NewLineBound(
                            corners[i], corners[3 == i ? 0 : i + 1]);

                        wall = createDoc.NewWall(
                            line, levelMiddle, false);

                        param = wall.get_Parameter(topLevelParam);
                        param.Set(ref topId);
                        walls.Add(wall);
                    }

                    List <Element> doorSymbols
                        = LabUtils.GetAllFamilySymbols(
                              app, BuiltInCategory.OST_Doors);

                    Debug.Assert(
                        0 < doorSymbols.Count,
                        "expected at least one door symbol"
                        + " to be loaded into project");

                    FamilySymbol door
                        = doorSymbols[0] as FamilySymbol;

                    XYZ midpoint = LabUtils.Midpoint(
                        corners[0], corners[1]);

                    FamilyInstance inst0
                        = createDoc.NewFamilyInstance(
                              midpoint, door, walls[0], levelBottom,
                              StructuralType.NonStructural);

                    midpoint.Z = levelMiddle.Elevation;

                    FamilyInstance inst1
                        = createDoc.NewFamilyInstance(
                              midpoint, door, walls[4], levelMiddle,
                              StructuralType.NonStructural);
#endif // CREATE_TWO_LEVELS
                    #endregion // Test creating two levels

                    // Determine the levels where the walls will be located:

                    Level levelBottom = null;
                    Level levelTop    = null;

                    if (!LabUtils.GetBottomAndTopLevels(doc, ref levelBottom, ref levelTop))
                    {
                        message = "Unable to determine wall bottom and top levels";
                        return(Result.Failed);
                    }
                    Debug.Print(string.Format("Drawing walls on '{0}' up to '{1}'",
                                              levelBottom.Name, levelTop.Name));

                    // Create the walls:

                    BuiltInParameter topLevelParam = BuiltInParameter.WALL_HEIGHT_TYPE;
                    ElementId        levelBottomId = levelBottom.Id;
                    ElementId        topLevelId    = levelTop.Id;
                    List <Wall>      walls         = new List <Wall>(4);

                    for (int i = 0; i < 4; ++i)
                    {
                        Line line = Line.CreateBound(corners[i], corners[3 == i ? 0 : i + 1]);
                        //Wall wall = createDoc.NewWall( line, levelBottom, false ); // 2012
                        Wall      wall  = Wall.Create(doc, line, levelBottomId, false); // 2013
                        Parameter param = wall.get_Parameter(topLevelParam);
                        param.Set(topLevelId);
                        walls.Add(wall);
                    }

                    // Determine wall thickness for tag offset and profile growth:

                    //double wallThickness = walls[0].WallType.CompoundStructure.Layers.get_Item( 0 ).Thickness; // 2011
                    //double wallThickness = walls[0].WallType.GetCompoundStructure().GetLayers()[0].Width; // 2012
                    double wallThickness = walls[0].WallType.Width; // simpler and more direct property available in 2012

                    // Add door and windows to the first wall;
                    // note that the NewFamilyInstance() api method does not automatically add door
                    // and window tags, like the ui command does. we add tags here by making additional calls
                    // to NewTag():

                    FamilySymbol door = LabUtils.GetFirstFamilySymbol(doc, BuiltInCategory.OST_Doors);
                    if (null == door)
                    {
                        LabUtils.InfoMsg("No door symbol found.");
                        return(Result.Failed);
                    }
                    FamilySymbol window = LabUtils.GetFirstFamilySymbol(
                        doc, BuiltInCategory.OST_Windows);

                    if (null == window)
                    {
                        LabUtils.InfoMsg("No window symbol found.");
                        return(Result.Failed);
                    }

                    XYZ    midpoint  = LabUtils.Midpoint(corners[0], corners[1]);
                    XYZ    p         = LabUtils.Midpoint(corners[0], midpoint);
                    XYZ    q         = LabUtils.Midpoint(midpoint, corners[1]);
                    double tagOffset = 3 * wallThickness;

                    //double windowHeight = 1 * LabConstants.MeterToFeet;
                    double windowHeight = levelBottom.Elevation + 0.3 * (
                        levelTop.Elevation - levelBottom.Elevation);

                    p = new XYZ(p.X, p.Y, windowHeight);
                    q = new XYZ(q.X, q.Y, windowHeight);
                    View view = doc.ActiveView;

                    door.Activate(); // 2016

                    FamilyInstance inst = createDoc.NewFamilyInstance(
                        midpoint, door, walls[0], levelBottom, StructuralType.NonStructural);

                    midpoint += tagOffset * XYZ.BasisY;

                    //IndependentTag tag = createDoc.NewTag(
                    //  view, inst, false, TagMode.TM_ADDBY_CATEGORY,
                    //  TagOrientation.Horizontal, midpoint ); // 2017

                    IndependentTag tag = IndependentTag.Create(
                        doc, view.Id, new Reference(inst),
                        false, TagMode.TM_ADDBY_CATEGORY,
                        TagOrientation.Horizontal, midpoint); // 2018

                    IList <FamilyPointPlacementReference> fpprefs = inst.GetFamilyPointPlacementReferences();
                    IList <Reference> refs = inst.GetReferences(FamilyInstanceReferenceType.CenterLeftRight);

                    //IndependentTag tag = IndependentTag.Create( doc,
                    //  view.Id, refs[0], false, TagMode.TM_ADDBY_CATEGORY,
                    //  TagOrientation.Horizontal, midpoint ); // 2018

                    window.Activate(); // 2016

                    inst = createDoc.NewFamilyInstance(p, window,
                                                       walls[0], levelBottom, StructuralType.NonStructural);

                    p += tagOffset * XYZ.BasisY;

                    //tag = createDoc.NewTag( view, inst,
                    //  false, TagMode.TM_ADDBY_CATEGORY,
                    //  TagOrientation.Horizontal, p ); // 2017

                    tag = IndependentTag.Create(
                        doc, view.Id, new Reference(inst),
                        false, TagMode.TM_ADDBY_CATEGORY,
                        TagOrientation.Horizontal, p); // 2018

                    inst = createDoc.NewFamilyInstance(q, window, walls[0],
                                                       levelBottom, StructuralType.NonStructural);

                    q += tagOffset * XYZ.BasisY;

                    //tag = createDoc.NewTag( view, inst,
                    //  false, TagMode.TM_ADDBY_CATEGORY,
                    //  TagOrientation.TAG_HORIZONTAL, q ); // 2011

                    //tag = createDoc.NewTag( view, inst,
                    //  false, TagMode.TM_ADDBY_CATEGORY,
                    //  TagOrientation.Horizontal, q ); // 2012

                    tag = IndependentTag.Create(
                        doc, view.Id, new Reference(inst),
                        false, TagMode.TM_ADDBY_CATEGORY,
                        TagOrientation.Horizontal, p); // 2018

                    // Grow the profile out by half the wall thickness,
                    // so the floor and roof do not stop halfway through the wall:

                    double w = 0.5 * wallThickness;
                    corners[0] -= w * (XYZ.BasisX + XYZ.BasisY);
                    corners[1] += w * (XYZ.BasisX - XYZ.BasisY);
                    corners[2] += w * (XYZ.BasisX + XYZ.BasisY);
                    corners[3] -= w * (XYZ.BasisX - XYZ.BasisY);
                    CurveArray profile = new CurveArray();
                    for (int i = 0; i < 4; ++i)
                    {
                        //Line line = createApp.NewLineBound( // 2013

                        Line line = Line.CreateBound( // 2014
                            corners[i], corners[3 == i ? 0 : i + 1]);

                        profile.Append(line);
                    }

                    // Add a floor, a roof and the roof slope:

                    bool  structural = false;
                    Floor floor      = createDoc.NewFloor(
                        profile, structural);

                    List <Element> roofTypes
                        = new List <Element>(
                              LabUtils.GetElementsOfType(
                                  doc, typeof(RoofType),
                                  BuiltInCategory.OST_Roofs));

                    Debug.Assert(0 < roofTypes.Count,
                                 "expected at least one roof type"
                                 + " to be loaded into project");

                    // Ensure that we get a valid roof type.
                    // In Revit 2013, the first one encountered
                    // is sloped glazing with zero entries in
                    // its compound layers; actually, the entire
                    // compound structure is null:

                    //RoofType roofType = null;
                    //foreach( RoofType rt in roofTypes )
                    //{
                    //  CompoundStructure cs = rt.GetCompoundStructure();
                    //  if( null != cs
                    //    && 0 < cs.GetLayers().Count )
                    //  {
                    //    roofType = rt;
                    //    break;
                    //  }
                    //}

                    RoofType roofType = roofTypes
                                        .Cast <RoofType>()
                                        .FirstOrDefault <RoofType>(typ
                                                                   => null != typ.GetCompoundStructure());

                    ModelCurveArray modelCurves
                        = new ModelCurveArray();

                    FootPrintRoof roof
                        = createDoc.NewFootPrintRoof(profile,
                                                     levelTop, roofType, out modelCurves);

                    // Regenerate the model after roof creation,
                    // otherwise the calls to set_DefinesSlope and
                    // set_SlopeAngle throw the exception "Unable
                    // to access curves from the roof sketch."

                    doc.Regenerate();

                    // The argument to set_SlopeAngle is NOT an
                    // angle, it is really a slope, i.e. relation
                    // of height to distance, e.g. 0.5 = 6" / 12",
                    // 0.75  = 9" / 12", etc.

                    double slope = 0.3;

                    foreach (ModelCurve curve in modelCurves)
                    {
                        roof.set_DefinesSlope(curve, true);
                        roof.set_SlopeAngle(curve, slope);
                    }

                    // Add a room and a room tag:

                    Room room = createDoc.NewRoom(levelBottom,
                                                  new UV(0.5 * width, 0.5 * depth));

                    //RoomTag roomTag = createDoc.NewRoomTag(
                    //  room, new UV( 0.5 * width, 0.7 * depth ),
                    //  null ); // 2014

                    RoomTag roomTag = createDoc.NewRoomTag(
                        new LinkElementId(room.Id),
                        new UV(0.5 * width, 0.7 * depth),
                        null); // 2015

                    //doc.AutoJoinElements(); // todo: remove this, the transaction should perform this automatically

                    //LabUtils.InfoMsg( "Little house was created successfully." );

                    //#region Test setting BaseOffset and LimitOffset
                    //// 11334196 [Failed to set Room.BaseOffset and Room.LimitOffset properties]
                    //double h = 0.123;
                    //room.BaseOffset = -h;
                    //room.LimitOffset = h + h;
                    //#endregion // Test setting BaseOffset and LimitOffset

                    t.Commit();

                    return(Result.Succeeded);
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }
        /// <summary>
        /// Improved implementation by Alexander Ignatovich
        /// supporting curved wall with curved window,
        /// second attempt, published April 10, 2015:
        /// </summary>
        public Result Execute3(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;
            View          view  = doc.ActiveView;

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

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

            Reference r = uidoc.Selection.PickObject(
                ObjectType.Element, "Select a wall");

            Element e = uidoc.Document.GetElement(r);

            Creator creator = new Creator(doc);

            Wall wall = e as Wall;

            if (wall == null)
            {
                return(Result.Cancelled);
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Wall Profile");

                // Get the external wall face for the profile
                // a little bit simpler than in the last
                // implementation in Execute2.

                Reference sideFaceReference
                    = HostObjectUtils.GetSideFaces(
                          wall, ShellLayerType.Exterior)
                      .First();

                Face face = wall.GetGeometryObjectFromReference(
                    sideFaceReference) as Face;

                // The plane and normal of the wall external face.

                XYZ       normal  = wall.Orientation.Normalize();
                Transform ftx     = face.ComputeDerivatives(UV.Zero);
                XYZ       forigin = ftx.Origin;
                XYZ       fnormal = ftx.BasisZ;

                Debug.Print(
                    "wall orientation {0}, face origin {1}, face normal {2}",
                    Util.PointString(normal),
                    Util.PointString(forigin),
                    Util.PointString(fnormal));

                // Offset distance.

                double d = 5;

                // Offset curve copies for visibility.

                XYZ       voffset = d * normal;
                Transform offset  = Transform.CreateTranslation(
                    voffset);

                // If the curve loop direction is counter-
                // clockwise, change its color to RED.

                Color colorRed = new Color(255, 0, 0);

                // Get edge loops as curve loops.

                IList <CurveLoop> curveLoops
                    = face.GetEdgesAsCurveLoops();

                foreach (var curveLoop in curveLoops)
                {
                    //CurveLoop curveLoopOffset = CurveLoop.CreateViaOffset(
                    //  curveLoop, d, normal );

                    CurveArray curves = creapp.NewCurveArray();

                    foreach (Curve curve in curveLoop)
                    {
                        curves.Append(curve.CreateTransformed(
                                          offset));
                    }

                    var isCounterClockwize = curveLoop
                                             .IsCounterclockwise(normal);

                    // Create model lines for an curve loop if it is made

                    Curve wallCurve = ((LocationCurve)wall.Location).Curve;

                    if (wallCurve is Line)
                    {
                        //Plane plane = creapp.NewPlane( curves ); // 2016

                        //Plane plane = curveLoopOffset.GetPlane(); // 2017

                        Plane plane = Plane.CreateByNormalAndOrigin( // 2019
                            normal, forigin + voffset);

                        Debug.Print(
                            "plane origin {0}, plane normal {1}",
                            Util.PointString(plane.Origin),
                            Util.PointString(plane.Normal));

                        SketchPlane sketchPlane
                            = SketchPlane.Create(doc, plane);

                        ModelCurveArray curveElements = credoc
                                                        .NewModelCurveArray(curves, sketchPlane);

                        if (isCounterClockwize)
                        {
                            SetModelCurvesColor(curveElements,
                                                view, colorRed);
                        }
                    }
                    else
                    {
                        foreach (var curve in curves.Cast <Curve>())
                        {
                            var curveElements = creator.CreateModelCurves(curve);
                            if (isCounterClockwize)
                            {
                                SetModelCurvesColor(curveElements, view, colorRed);
                            }
                        }
                    }
                }
                tx.Commit();
            }
            return(Result.Succeeded);
        }
        public ModelCurveArray CreateModelCurves(
            Curve curve)
        {
            var array = new ModelCurveArray();

              var line = curve as Line;
              if( line != null )
              {
            array.Append( CreateModelLine( _doc,
              curve.GetEndPoint( 0 ),
              curve.GetEndPoint( 1 ) ) );

            return array;
              }

              var arc = curve as Arc;
              if( arc != null )
              {
            var origin = arc.Center;
            var normal = arc.Normal;

            array.Append( CreateModelCurve(
              arc, origin, normal ) );

            return array;
              }

              var ellipse = curve as Ellipse;
              if( ellipse != null )
              {
            var origin = ellipse.Center;
            var normal = ellipse.Normal;

            array.Append( CreateModelCurve(
              ellipse, origin, normal ) );

            return array;
              }

              var points = curve.Tessellate();
              var p = points.First();

              foreach( var q in points.Skip( 1 ) )
              {
            array.Append( CreateModelLine( _doc, p, q ) );
            p = q;
              }

              return array;
        }