//create building pads
        public Autodesk.Revit.DB.Architecture.BuildingPad CreateBuildingPad(ProcessPolygon polygon, double elevation)
        {
            Level            level            = _doc.Create.NewLevel(elevation);
            List <CurveLoop> listOfCurveLoops = new List <CurveLoop>();

            listOfCurveLoops.Add(polygon.Get_CurveLoop());
            return(Autodesk.Revit.DB.Architecture.BuildingPad.Create(_doc, this.buildingPadType.Id, level.Id, listOfCurveLoops));
        }
Example #2
0
        public List <ElementId> Visualize(Document doc, Polygon polygon, double elevation, int exponent)
        {
            List <XYZ> points = new List <XYZ>();

            points = this.Polygon2XYZList(polygon, elevation, exponent);
            ProcessPolygon processedContour = new ProcessPolygon(points, true);

            processedContour.LoadEdgeLengths();
            //MessageBox.Show(string.Format("Minimum distance is {0} \n Maximum distance is {1}", processedContour.MinimumEdgeLength.ToString(), processedContour.MaximumEdgeLength.ToString()));
            return(processedContour.Visualize(doc));
        }
Example #3
0
        /// <summary>
        /// Convert Polygons to a list of curveloops
        /// </summary>
        public CurveLoops PolygonsToCurveLoops(Polygons polygons)
        {
            CurveLoops curveLoops = new CurveLoops();

            foreach (Polygon polygon in polygons)
            {
                ProcessPolygon processedPolygon = new ProcessPolygon(this.Polygon2XYZList(polygon, 0.0, this.Exponent), true);
                processedPolygon.RemoveIdenticalPoints();
                processedPolygon.RemoveClosePoints(.5);
                processedPolygon.ForceToFixList();
                processedPolygon.RemoveCollinearity(4 * Math.PI / 180);
                if (processedPolygon.ProcessedPolygon.Count > 2)
                {
                    curveLoops.Add(processedPolygon.Get_CurveLoop());
                }
            }
            return(curveLoops);
        }
Example #4
0
        public DataToTopography(Document document, List <List <XYZ> > contours, double interpolationLength = 10, double proximityTolerance = 5)
        {
            Dictionary <string, XYZ> dict = new Dictionary <string, XYZ>();
            int counter = 0;

            foreach (List <XYZ> cntr in contours)
            {
                ProcessPolygon processedContour = new ProcessPolygon(cntr);
                processedContour.LoadEdgeLengths();
                processedContour.RemoveClosePoints(proximityTolerance);
                processedContour.Interpolate(interpolationLength);
                processedContour.LoadEdgeLengths();
                foreach (XYZ item in processedContour.ProcessedPolygon)
                {
                    string key = item.X.ToString() + item.Y.ToString();
                    try
                    {
                        dict.Add(key, item);
                    }
                    catch (Exception)
                    {
                        //MessageBox.Show(er.Message);
                        counter++;
                    }
                }
            }
            this.NumberOfFailedPoints = counter;
            List <XYZ> pnts = new List <XYZ>();

            foreach (var item in dict.Keys)
            {
                XYZ pn;
                if (dict.TryGetValue(item, out pn))
                {
                    pnts.Add(pn);
                }
            }
            using (Transaction createTopography = new Transaction(document))
            {
                createTopography.Start("Create Topography");
                this.Topography = TopographySurface.Create(document, pnts);
                createTopography.Commit();
            }
        }
        // finding building elevation
        public double FindBuildingElevation(ProcessPolygon polygon)
        {
            List <XYZ>           heights        = new List <XYZ>();
            ReferenceIntersector refIntersector = new ReferenceIntersector(this.TopographyID, FindReferenceTarget.Mesh, this.RayTracerView);
            int    counter = 0;
            double h       = 0;

            foreach (XYZ item in polygon.ProcessedPolygon)
            {
                ReferenceWithContext referenceWithContext = refIntersector.FindNearest(item, XYZ.BasisZ);
                if (referenceWithContext != null)
                {
                    counter++;
                    h += referenceWithContext.Proximity;
                }
                else
                {
                    return(-1);
                }
            }
            return(h / counter);
        }
        public DataToBuilding(Document doc, List <Building> buildings, ElementId topographyID, string documentFileAddress = "")
        {
            this.DocumentFileAddress       = documentFileAddress;
            this.opt.OverwriteExistingFile = true;
            List <double> bldgHeight = new List <double>();

            _doc = doc; this.TopographyID = topographyID;
            using (Transaction createBuildingPadType = new Transaction(_doc))
            {
                createBuildingPadType.Start("Create Building Pad Type");
                this.buildingPadType = BuildingPadType.CreateDefault(_doc);
                createBuildingPadType.Commit();
            }
            this.FailedAttemptsToCreateBuildingPads = 0;
            this.FailedAttemptsToCreateBuildings    = 0;

            #region pre-process the contour lines
            List <ProcessPolygon> ProcessedFootPrints = new List <ProcessPolygon>();
            this.RayTracerView = this.createView3d();
            foreach (Building building in buildings)
            {
                ProcessPolygon processedContour = new ProcessPolygon(building.vertices);
                processedContour.Flatten();
                processedContour.RemoveIdenticalPoints();
                processedContour.RemoveClosePoints(.5);
                processedContour.RemoveCollinearity();
                processedContour.ForceToFixList();

                bldgHeight.Add(building.height);
                ProcessedFootPrints.Add(processedContour);
            }
            this.BuildingFootPrints = ProcessedFootPrints;
            #endregion

            #region Create building Masses ad independent families

            this.FamilyTemplateAddress = this.familyTemplateAddress();
            List <double> elevations = new List <double>();
            for (int i = 0; i < this.BuildingFootPrints.Count; i++)
            {
                elevations.Add(this.FindBuildingElevation(this.BuildingFootPrints[i]));
            }
            //Finding a folder to save the buildings
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            DialogResult        result        = folderBrowser.ShowDialog();
            string folder = "";
            if (result == DialogResult.OK)
            {
                folder = folderBrowser.SelectedPath;
            }
            //creating extrusions and saving them
            List <XYZ>          translations     = new List <XYZ>();
            List <FamilySymbol> symbols          = new List <FamilySymbol>();
            List <int>          footPrintIndices = new List <int>();

            for (int i = 0; i < 5; i++)
            {
                if (elevations[i] != double.NaN && elevations[i] != -1)
                {
                    string path        = folder + @"\" + i.ToString() + ".rfa";
                    XYZ    translation = XYZ.Zero;
                    foreach (XYZ item in this.BuildingFootPrints[i].ProcessedPolygon)
                    {
                        translation += item;
                    }
                    translation /= this.BuildingFootPrints[i].ProcessedPolygon.Count;

                    bool formCreated = CreateFamilyFile(this.BuildingFootPrints[i], bldgHeight[i], path, translation);
                    if (formCreated)
                    {
                        using (Transaction placeFamily = new Transaction(_doc))
                        {
                            FailureHandlingOptions failOpt = placeFamily.GetFailureHandlingOptions();
                            failOpt.SetFailuresPreprocessor(new WarningSwallower());
                            placeFamily.SetFailureHandlingOptions(failOpt);
                            placeFamily.Start("Place a Mass");
                            Family family = null;
                            _doc.LoadFamily(path, out family);
                            FamilySymbol symbol = null;
                            //foreach( FamilySymbol s in family.Symbols ) // 2014
                            foreach (ElementId id in family.GetFamilySymbolIds()) // 2015
                            {
                                symbol = _doc.GetElement(id) as FamilySymbol;
                                break;
                            }
                            symbols.Add(symbol);
                            XYZ displacement = new XYZ(translation.X, translation.Y, elevations[i]);
                            translations.Add(displacement);
                            footPrintIndices.Add(i);
                            placeFamily.Commit();
                        }
                    }
                    else
                    {
                        this.BuildingFootPrints[i].Visualize(_doc);
                    }
                }
            }
            #endregion

            #region inserting a mass file
            for (int i = 0; i < translations.Count; i++)
            {
                using (Transaction placeMass = new Transaction(_doc))
                {
                    FailureHandlingOptions failOpt = placeMass.GetFailureHandlingOptions();
                    failOpt.SetFailuresPreprocessor(new WarningSwallower());
                    placeMass.SetFailureHandlingOptions(failOpt);
                    placeMass.Start("Insert Mass");
                    try
                    {
                        FamilyInstance building = _doc.Create.NewFamilyInstance(XYZ.Zero, symbols[i], StructuralType.NonStructural);
                        ElementTransformUtils.MoveElement(_doc, building.Id, translations[i]);
                        this.BuildingMasses.Add(building);
                    }
                    catch (Exception)
                    {
                        this.FailedAttemptsToCreateBuildings++;
                    }
                    placeMass.Commit();
                }
            }
            #endregion

            if (this.DocumentFileAddress != "")
            {
                _doc.SaveAs(this.DocumentFileAddress, this.opt);
            }

            #region creating building pads
            //for (int i = 0; i < footPrintIndices.Count; i++)
            //{
            //    Transaction CreatePads = new Transaction(_doc, "Create Building Pad");
            //    FailureHandlingOptions failOpt = CreatePads.GetFailureHandlingOptions();
            //    failOpt.SetFailuresPreprocessor(new WarningSwallower());
            //    CreatePads.SetFailureHandlingOptions(failOpt);
            //    CreatePads.Start();
            //    try
            //    {
            //        Autodesk.Revit.DB.Architecture.BuildingPad pad = this.CreateBuildingPad(this.BuildingFootPrints[footPrintIndices[i]], elevations[footPrintIndices[i]]);
            //        this.BuildingPads.Add(pad);
            //        if (this.DocumentFileAddress != "")
            //        {
            //            _doc.SaveAs(this.DocumentFileAddress, this.opt);
            //        }
            //    }
            //    catch (Exception)
            //    {
            //        this.FailedAttemptsToCreateBuildingPads++;
            //    }

            //    CreatePads.Commit();
            //}
            #endregion
        }
        //Create family for each building
        //http://thebuildingcoder.typepad.com/blog/2011/06/creating-and-inserting-an-extrusion-family.html
        private bool CreateFamilyFile(ProcessPolygon polygon, double height, string familyFileName, XYZ translation)
        {
            bool     success = true;
            Document FamDoc  = null;

            Autodesk.Revit.DB.Form form = null;
            using (Transaction CreateFamily = new Transaction(_doc))
            {
                CreateFamily.Start("Create a new Family");
                FamDoc = _doc.Application.NewFamilyDocument(this.FamilyTemplateAddress);
                CreateFamily.Commit();
            }
            ReferenceArray refAr = polygon.Get_ReferenceArray(FamDoc, translation);

            using (Transaction CreateExtrusion = new Transaction(FamDoc))
            {
                FailureHandlingOptions failOpt = CreateExtrusion.GetFailureHandlingOptions();
                failOpt.SetFailuresPreprocessor(new WarningSwallower());
                CreateExtrusion.SetFailureHandlingOptions(failOpt);
                CreateExtrusion.Start("Create Extrusion");
                /*Mohammad took this out of try */
                try
                {
                    form = FamDoc.FamilyCreate.NewExtrusionForm(true, refAr, height * XYZ.BasisZ);
                }
                catch (Exception)
                {
                    this.FailedAttemptsToCreateBuildings++;
                    success = false;
                }
                CreateExtrusion.Commit();
            }

            //Added by Mohammad
            using (Transaction AddParamTrans = new Transaction(FamDoc))
            {
                AddParamTrans.Start("Add Parameter");
                Autodesk.Revit.ApplicationServices.Application app = FamDoc.Application;
                View3D view3d = createView3d();

                Dimension windowInsetDimension = null;
                FaceArray faces = new FaceArray();
                if (form.IsSolid)
                {
                    Options options = new Options();
                    options.ComputeReferences = true;
                    //options.View = new Autodesk.Revit.DB.View();
                    //GeometryObjectArray geoArr = extrusion.get_Geometry(options).Objects;
                    IEnumerator <GeometryObject> Objects = form.get_Geometry(options).GetEnumerator();
                    //foreach (GeometryObject geoObj in geoArr)
                    while (Objects.MoveNext())
                    {
                        GeometryObject geoObj = Objects.Current;

                        if (geoObj is Solid)
                        {
                            Solid s = geoObj as Solid;
                            foreach (Face fc in s.Faces)
                            {
                                //MessageBox.Show(fc.ComputeNormal(new UV(0, 0)).X.ToString() + "/n" + fc.ComputeNormal(new UV(0, 0)).Y.ToString() + "/n" + fc.ComputeNormal(new UV(0, 0)).Z.ToString());
                                if (Math.Round((fc.ComputeNormal(new UV(0, 0))).Z) == 1 || Math.Round((fc.ComputeNormal(new UV(0, 0))).Z) == -1)
                                {
                                    faces.Append(fc);
                                }
                            }
                            //**************************************************************************************************************
                            //****************************Here is the Error **********************************************************************
                            //************************************************************************************************************************
                            //windowInsetDimension = AddDimension( FamDoc, view3d, faces.get_Item( 0 ), faces.get_Item( 1 ) );
                            View viewElevation = new FilteredElementCollector(FamDoc).OfClass(typeof(View)).Cast <View>().Where <View>(v => ViewType.Elevation == v.ViewType).FirstOrDefault <View>();
                            windowInsetDimension = AddDimension(FamDoc, viewElevation, faces.get_Item(0), faces.get_Item(1));
                        }
                    }
                }

                //Test for creating dimension
                #region two lines creating dimenstion
                //// first create two lines
                //XYZ pt1 = new XYZ(5, 0, 5);
                //XYZ pt2 = new XYZ(5, 0, 10);
                //Line line = Line.CreateBound(pt1, pt2);
                //Plane plane = app.Create.NewPlane(pt1.CrossProduct(pt2), pt2);

                //SketchPlane skplane = SketchPlane.Create(FamDoc, plane);

                //ModelCurve modelcurve1 = FamDoc.FamilyCreate.NewModelCurve(line, skplane);

                //pt1 = new XYZ(10, 0, 5);
                //pt2 = new XYZ(10, 0, 10);
                //line = Line.CreateBound(pt1, pt2);
                //plane = app.Create.NewPlane(pt1.CrossProduct(pt2), pt2);

                //skplane = SketchPlane.Create(FamDoc, plane);

                //ModelCurve modelcurve2 = FamDoc.FamilyCreate.NewModelCurve(line, skplane);



                //// now create a linear dimension between them
                //ReferenceArray ra = new ReferenceArray();
                //ra.Append(modelcurve1.GeometryCurve.Reference);
                //ra.Append(modelcurve2.GeometryCurve.Reference);

                //pt1 = new XYZ(5, 0, 10);
                //pt2 = new XYZ(10, 0, 10);
                //line = Line.CreateBound(pt1, pt2);


                //Dimension dim = FamDoc.FamilyCreate.NewLinearDimension(view3d, line, ra);
                #endregion

                //creates a prameter named index for each family.
                BuiltInParameterGroup paramGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), "PG_GENERAL");
                ParameterType         paramType  = (ParameterType)Enum.Parse(typeof(ParameterType), "Length");
                FamilyManager         m_manager  = FamDoc.FamilyManager;

                FamilyParameter famParam = m_manager.AddParameter("Height", paramGroup, paramType, true);

                //Set the value for the parameter
                if (m_manager.Types.Size == 0)
                {
                    m_manager.NewType("Type 1");
                }

                m_manager.Set(famParam, height);

                //connects dimension to lable called with
                //windowInsetDimension.FamilyLabel = famParam;

                AddParamTrans.Commit();
            }


            SaveAsOptions opt = new SaveAsOptions();
            opt.OverwriteExistingFile = true;
            FamDoc.SaveAs(familyFileName, opt);
            FamDoc.Close(false);
            return(success);
        }