Example #1
0
        private void CreateInstances(GeometryElement geoElem, Document doc, string selectedLayerItem, IList <Element> allLevels, IList <ColumnsTobeRotated> fInnstances)
        {
            double angle = 0;

            using (Transaction tCreate = new Transaction(doc, Properties.Messages.BeamsFromCAD_Transaction))
            {
                tCreate.Start();
                foreach (GeometryObject geoObj in geoElem)
                {
                    if (geoObj is GeometryInstance)
                    {
                        GeometryInstance geoInstance = geoObj as GeometryInstance;
                        GeometryElement  geo         = geoInstance.GetInstanceGeometry();

                        foreach (var currentGeo in geo)
                        {
                            #region if Poly Line
                            if (currentGeo is PolyLine)
                            {
                                PolyLine currentPolyLine = currentGeo as PolyLine;
                                int      numCoordinates  = currentPolyLine.NumberOfCoordinates;

                                if (numCoordinates == 5)
                                {
                                    IList <XYZ> coordinates = currentPolyLine.GetCoordinates();
                                    if ((((doc.GetElement(currentPolyLine.GraphicsStyleId) as GraphicsStyle).GraphicsStyleCategory).Name) != selectedLayerItem)
                                    {
                                        continue;
                                    }

                                    IList <XYZ> organizedPoints = new List <XYZ>();
                                    XYZ         pointOfInterest = new XYZ(-999, 999, 0);

                                    Line line1     = Line.CreateBound(coordinates.ElementAt(0), coordinates.ElementAt(1));
                                    XYZ  direction = line1.Direction;

                                    Line line2 = Line.CreateBound(coordinates.ElementAt(1), coordinates.ElementAt(2));

                                    if ((direction.X < 0) && (direction.Y < 0))
                                    {
                                        direction = new XYZ(direction.X * (-1), direction.Y, direction.Z);
                                        angle     = direction.AngleTo(XYZ.BasisY) + (Math.PI / 2);
                                    }
                                    else if (direction.X < 0)
                                    {
                                        angle = direction.AngleTo(XYZ.BasisY) + (Math.PI / 2);
                                    }
                                    else if (direction.Y < 0)
                                    {
                                        if (line1.ApproximateLength < line2.ApproximateLength)
                                        {
                                            direction = new XYZ(direction.X, direction.Y * (-1), direction.Z);
                                            angle     = direction.AngleTo(XYZ.BasisX) + (Math.PI / 2);
                                            if (direction.Y == 1)
                                            {
                                                angle = angle + (Math.PI / 2);
                                            }
                                        }
                                        else
                                        {
                                            direction = new XYZ(direction.X, direction.Y * (-1), direction.Z);
                                            angle     = direction.AngleTo(XYZ.BasisY) + (Math.PI / 2);
                                        }
                                    }
                                    else if ((direction.X > 0) || (direction.Y > 0))
                                    {
                                        angle = direction.AngleTo(XYZ.BasisX);
                                    }

                                    double angleG        = Utils.ConvertM.radiansToDegrees(angle);
                                    double line1Length   = line1.ApproximateLength;
                                    double line1LengthCm = Math.Round(Utils.ConvertM.feetToCm(line1Length), 1);

                                    if (line1LengthCm < minLineLengthInCm || line1LengthCm > maxLineLengthInCm)
                                    {
                                        continue;
                                    }

                                    XYZ    line2Direct   = line2.Direction;
                                    double angle22       = Utils.ConvertM.radiansToDegrees(line2Direct.AngleTo(XYZ.BasisX));
                                    double line2Length   = line2.ApproximateLength;
                                    double line2LengthCm = Math.Round(Utils.ConvertM.feetToCm(line2Length), 1);

                                    if (line2LengthCm < minLineLengthInCm || line2LengthCm > maxLineLengthInCm)
                                    {
                                        continue;
                                    }

                                    IList <XYZ> first3Points = new List <XYZ>();
                                    for (int i = 0; i < 3; i++)
                                    {
                                        first3Points.Add(coordinates.ElementAt(i));
                                    }

                                    double maxDist = double.NegativeInfinity;
                                    XYZ    point1  = new XYZ();
                                    XYZ    point2  = new XYZ();

                                    foreach (XYZ currentPoint1 in first3Points)
                                    {
                                        foreach (XYZ currentPoint2 in first3Points)
                                        {
                                            double currentDist = currentPoint1.DistanceTo(currentPoint2);
                                            if (currentDist > maxDist)
                                            {
                                                maxDist = currentDist;
                                                point1  = currentPoint1;
                                                point2  = currentPoint2;
                                            }
                                        }
                                    }

                                    FamilyInstance  fi             = null;
                                    IList <Element> createdColumns = new List <Element>();

                                    for (int l = 0; l < ONBOXApplication.StoredColumnsDwgLevels.Count; l++)
                                    {
                                        if (ONBOXApplication.StoredColumnsDwgLevels.ElementAt(l).willBeNumbered == false)
                                        {
                                            continue;
                                        }

                                        XYZ             midPoint    = Utils.GetPoint.getMidPoint(point1, point2);
                                        FamilyWithImage familyInfo  = ONBOXApplication.storedColumnFamiliesInfo.ElementAt(ONBOXApplication.selectedColumnFamily);
                                        FamilySymbol    fs          = uidoc.Document.GetElement((uidoc.Document.GetElement(new ElementId(familyInfo.FamilyID)) as Family).GetFamilySymbolIds().First()) as FamilySymbol;
                                        string          newTypeName = (line1LengthCm).ToString() + " x " + (line2LengthCm).ToString() + "cm";
                                        ElementType     newType     = null;

                                        if (!Utils.FindElements.CheckTypeForDuplicate(newTypeName, fs.Family, doc))
                                        {
                                            newType = fs.Duplicate(newTypeName);
                                            newType.LookupParameter("b").Set(line1Length);
                                            newType.LookupParameter("h").Set(line2Length);
                                        }
                                        else
                                        {
                                            newType = Utils.FindElements.GetFamilySymbol(newTypeName, fs.Family, doc);
                                        }

                                        FamilySymbol fs2 = newType as FamilySymbol;
                                        fi = doc.Create.NewFamilyInstance(midPoint, fs2, (allLevels.First() as Level), Autodesk.Revit.DB.Structure.StructuralType.Column);

                                        ElementId topLevelID  = new ElementId(ONBOXApplication.StoredColumnsDwgLevels.ElementAt(l + 1).levelId);
                                        ElementId baseLevelID = new ElementId(ONBOXApplication.StoredColumnsDwgLevels.ElementAt(l).levelId);

                                        fi.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM).Set(baseLevelID);
                                        fi.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).Set(topLevelID);

                                        fInnstances.Add(new ColumnsTobeRotated {
                                            currentInstance = fi, currentAngle = angle
                                        });
                                    }
                                }
                            }
                            #endregion

                            #region if Arc - Circle
                            if (currentGeo is Arc)
                            {
                                Arc currentArc = currentGeo as Arc;

                                if ((currentArc.IsCyclic == false) || (currentArc.IsBound == true))
                                {
                                    continue;
                                }
                                if ((((doc.GetElement(currentArc.GraphicsStyleId) as GraphicsStyle).GraphicsStyleCategory).Name) != selectedLayerItem)
                                {
                                    continue;
                                }

                                XYZ currentArcLocation = currentArc.Center;
                                //currentArcLocation = dwgTransform.OfPoint(currentArcLocation);
                                double currentArcDiameter   = currentArc.Radius * 2;
                                string currentArcDiameterCm = Math.Round(Utils.ConvertM.feetToCm(currentArcDiameter)).ToString();
                                //currentArcLocation = dwgTransform.OfPoint(currentArcLocation);

                                foreach (LevelInfo currentLevelInfo in ONBOXApplication.StoredColumnsDwgLevels)
                                {
                                    if (currentLevelInfo.willBeNumbered == false)
                                    {
                                        continue;
                                    }

                                    FamilyWithImage familyInfo  = ONBOXApplication.storedColumnFamiliesCircInfo.ElementAt(ONBOXApplication.selectedColumnCircFamily);
                                    FamilySymbol    fs          = uidoc.Document.GetElement((uidoc.Document.GetElement(new ElementId(familyInfo.FamilyID)) as Family).GetFamilySymbolIds().First()) as FamilySymbol;
                                    string          newTypeName = currentArcDiameterCm + "cm";
                                    ElementType     newType     = null;

                                    if (!Utils.FindElements.CheckTypeForDuplicate(newTypeName, fs.Family, doc))
                                    {
                                        newType = fs.Duplicate(newTypeName);
                                        newType.LookupParameter("b").Set(currentArcDiameter);
                                        if (newType.LookupParameter("h") != null)
                                        {
                                            newType.LookupParameter("h").Set(currentArcDiameter);
                                        }
                                    }
                                    else
                                    {
                                        newType = Utils.FindElements.GetFamilySymbol(newTypeName, fs.Family, doc);
                                    }

                                    FamilySymbol   fs2 = newType as FamilySymbol;
                                    FamilyInstance fi  = doc.Create.NewFamilyInstance(currentArcLocation, fs2, (allLevels.First() as Level), Autodesk.Revit.DB.Structure.StructuralType.Column);

                                    ElementId topLevelID  = new ElementId((ONBOXApplication.StoredColumnsDwgLevels.ElementAt(ONBOXApplication.StoredColumnsDwgLevels.IndexOf(currentLevelInfo) + 1)).levelId);
                                    ElementId baseLevelID = new ElementId(currentLevelInfo.levelId);

                                    fi.get_Parameter(BuiltInParameter.FAMILY_BASE_LEVEL_PARAM).Set(baseLevelID);
                                    fi.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_PARAM).Set(topLevelID);

                                    fInnstances.Add(new ColumnsTobeRotated {
                                        currentInstance = fi, currentAngle = angle
                                    });
                                }
                            }
                            #endregion
                        }
                    }
                }
                tCreate.Commit();
            }
        }
Example #2
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            doc = uidoc.Document;
            Selection sel = uidoc.Selection;

            try
            {
                IList <FamilyWithImage> allBeamsInfo = Utils.GetInformation.GetAllBeamFamilies(doc);

                if (allBeamsInfo.Count < 1)
                {
                    message = Properties.Messages.BeamsForBuilding_NoBeamFamilyLoaded;
                    return(Result.Failed);
                }

                BeamsFromEntireBuildingUI currentUI = new BeamsFromEntireBuildingUI(this, allBeamsInfo);

                currentUI.ShowDialog();

                if (currentUI.DialogResult == false)
                {
                    return(Result.Cancelled);
                }

                FamilyWithImage currentFamilyWithImage = currentUI.CurrentFamilyWithImage;
                Family          currentFamily          = doc.GetElement(new ElementId(currentFamilyWithImage.FamilyID)) as Family;
                //for now we will set the symbol for the first beam of that family
                //later on we will duplicate it and check if it exist or not
                ElementId         fsID       = currentFamily.GetFamilySymbolIds().First();
                FamilySymbol      fs         = doc.GetElement(fsID) as FamilySymbol;
                double            beamHeight = Utils.ConvertM.cmToFeet(currentUI.BeamHeight);
                double            beamWidth  = Utils.ConvertM.cmToFeet(currentUI.BeamWidth);
                bool              createBeamsInIntermediateLevels = currentUI.CreateBeamsInIntermediateLevels;
                bool              ignoreStandardLevels            = currentUI.GroupAndDuplicateLevels;
                bool              isLinked                 = currentUI.IsLinked;
                double            minWallWidth             = Utils.ConvertM.cmToFeet(currentUI.MinWallWidth);
                IList <LevelInfo> allLevelInfo             = currentUI.LevelInfoList;
                string            standardLevelName        = currentUI.StandardLevelName;
                bool              pickStandardLevelsByName = currentUI.PickStandardLevelsByName;
                bool              isCaseSensitive          = currentUI.IsCaseSensitive;
                bool              isJoinBeams              = (bool)currentUI.checkJoinBeams.IsChecked;

                //Will be set if needed
                int            linkedInstanceID     = -1;
                Transform      linkedInstanceTransf = null;
                IList <string> listOfNames          = new List <string>();
                listOfNames.Add(standardLevelName);

                if (isLinked == false)
                {
                    currentDoc = doc;
                }
                else
                {
                    RevitLinkInstance rvtInstance = doc.GetElement(new ElementId(currentUI.SelectedRevitLinkInfo.Id)) as RevitLinkInstance;
                    currentDoc           = rvtInstance.GetLinkDocument();
                    linkedInstanceTransf = rvtInstance.GetTotalTransform();
                    linkedInstanceID     = currentUI.SelectedRevitLinkInfo.Id;
                }

                if (pickStandardLevelsByName)
                {
                    foreach (LevelInfo currentLevelInfo in allLevelInfo)
                    {
                        if (LevelNameContainsString(currentLevelInfo.levelName, listOfNames, isCaseSensitive))
                        {
                            currentLevelInfo.isStandardLevel = true;
                        }
                    }
                }

                string       beamWidthInCm       = Math.Round(Utils.ConvertM.feetToCm(beamWidth)).ToString();
                string       beamHeigthInCm      = Math.Round(Utils.ConvertM.feetToCm(beamHeight)).ToString();
                string       newTypeName         = beamWidthInCm + " x " + beamHeigthInCm + "cm";
                FamilySymbol currentFamilySymbol = null;

                using (Transaction t = new Transaction(doc, Properties.Messages.BeamsForBuilding_Transaction))
                {
                    t.Start();

                    if (!Utils.FindElements.thisTypeExist(newTypeName, currentFamily.Name, BuiltInCategory.OST_StructuralFraming, doc))
                    {
                        currentFamilySymbol = fs.Duplicate(newTypeName) as FamilySymbol;

                        Parameter parameterB = currentFamilySymbol.LookupParameter("b");
                        Parameter parameterH = currentFamilySymbol.LookupParameter("h");

                        //TODO check for code like this that can throw exceptions
                        parameterB.Set(beamWidth);
                        parameterH.Set(beamHeight);
                    }
                    else
                    {
                        currentFamilySymbol = Utils.FindElements.findElement(newTypeName, currentFamily.Name, BuiltInCategory.OST_StructuralFraming, doc) as FamilySymbol;
                    }

                    currentFamilySymbol.Activate();
                    bool isThisTheFirstStandardLevel = true;

                    foreach (LevelInfo currentLevelInfo in allLevelInfo)
                    {
                        if (currentLevelInfo == null)
                        {
                            continue;
                        }

                        if (!currentLevelInfo.willBeNumbered)
                        {
                            continue;
                        }

                        Level currentLevel = currentDoc.GetElement(new ElementId(currentLevelInfo.levelId)) as Level;

                        XYZ minPoint = new XYZ(-9999, -9999, currentLevel.ProjectElevation - 0.1);
                        XYZ maxPoint = new XYZ(9999, 9999, currentLevel.ProjectElevation + 0.1);

                        Outline levelOutLine = new Outline(minPoint, maxPoint);
                        BoundingBoxIntersectsFilter levelBBIntersect = new BoundingBoxIntersectsFilter(levelOutLine, 0.05);
                        BoundingBoxIsInsideFilter   levelBBInside    = new BoundingBoxIsInsideFilter(levelOutLine, 0.05);

                        LogicalOrFilter levelInsideOrIntersectFilter = new LogicalOrFilter(levelBBInside, levelBBIntersect);

                        IList <Element> wallsThatIntersectsCurrentLevel = new FilteredElementCollector(currentDoc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()
                                                                          .WherePasses(levelInsideOrIntersectFilter).ToList();

                        if (currentLevelInfo.isStandardLevel && ignoreStandardLevels)
                        {
                            if (isThisTheFirstStandardLevel)
                            {
                                isThisTheFirstStandardLevel = false;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        foreach (Element currentWallElment in wallsThatIntersectsCurrentLevel)
                        {
                            Wall currentWall = currentWallElment as Wall;

                            //wall is valid?
                            if (currentWall == null)
                            {
                                continue;
                            }

                            if (currentWall.Width < minWallWidth)
                            {
                                continue;
                            }

                            if (!createBeamsInIntermediateLevels)
                            {
                                if (currentWallElment.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId() != currentLevel.Id &&
                                    currentWallElment.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId() != currentLevel.Id)
                                {
                                    continue;
                                }
                            }

                            LocationCurve wallLocationCurve = currentWall.Location as LocationCurve;

                            //we need to verify if this wall has locationCurve
                            if (wallLocationCurve == null)
                            {
                                continue;
                            }

                            Curve wallCurve = ((currentWall.Location) as LocationCurve).Curve;

                            if (isLinked)
                            {
                                wallCurve = wallCurve.CreateTransformed(linkedInstanceTransf);
                            }

                            double wallBaseOffset = currentWall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble();

                            Level wallBaseLevel = currentDoc.GetElement(currentWall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId()) as Level;

                            double wallBaseTotalHeight = wallBaseLevel.ProjectElevation + wallBaseOffset;

                            double wallTotalHeight = currentWall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();

                            if (wallTotalHeight < beamHeight)
                            {
                                continue;
                            }

                            double levelHeight           = currentLevel.ProjectElevation;
                            Level  currentLevelInProject = null;

                            if (isLinked)
                            {
                                levelHeight += linkedInstanceTransf.Origin.Z;

                                IList <Level> allLevelsInProject = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).OfClass(typeof(Level)).Cast <Level>().ToList();

                                foreach (Level levelCanditate in allLevelsInProject)
                                {
                                    if (Math.Abs(levelHeight - levelCanditate.ProjectElevation) <= 0.1)
                                    {
                                        currentLevelInProject = levelCanditate;
                                        break;
                                    }
                                }

                                if (currentLevelInProject == null)
                                {
                                    currentLevelInProject = Level.Create(doc, levelHeight);
                                }
                            }
                            else
                            {
                                currentLevelInProject = currentLevel;
                            }

                            FamilyInstance currentBeamInstance = doc.Create.NewFamilyInstance(wallCurve, currentFamilySymbol, currentLevelInProject, Autodesk.Revit.DB.Structure.StructuralType.Beam);

                            AdjustBeamParameters(currentBeamInstance, currentLevelInProject);

                            doc.Regenerate();
                            Utils.CheckFamilyInstanceForIntersection.CheckForDuplicatesAndIntersectingBeams(currentBeamInstance, doc);
                        }
                    }

                    if (isJoinBeams)
                    {
                        IList <FamilyInstance> allBeamsInstances = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFraming)
                                                                   .OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType().Cast <FamilyInstance>().ToList();

                        foreach (FamilyInstance currentBeam in allBeamsInstances)
                        {
                            Utils.CheckFamilyInstanceForIntersection.JoinBeamToWalls(currentBeam, doc);
                        }
                    }


                    t.Commit();
                }
            }
            catch (Exception excep)
            {
                ExceptionManager eManager = new ExceptionManager(excep);
            }
            return(Result.Succeeded);
        }