Ejemplo n.º 1
0
        private static void Process(DesignAutomationData data)
        {
            if (data == null)
            {
                throw new InvalidDataException(nameof(data));
            }

            Application rvtApp = data.RevitApp;

            if (rvtApp == null)
            {
                throw new InvalidDataException(nameof(rvtApp));
            }

            System.Console.WriteLine("Start application...");

            System.Console.WriteLine("File Path" + data.FilePath);


            string modelPath = data.FilePath;

            if (String.IsNullOrWhiteSpace(modelPath))
            {
                System.Console.WriteLine("No File Path");
            }

            if (String.IsNullOrWhiteSpace(modelPath))
            {
                throw new InvalidDataException(nameof(modelPath));
            }

            Document doc = data.RevitDoc;

            if (doc == null)
            {
                throw new InvalidOperationException("Could not open document.");
            }

            // SetUnits(doc);

            string          filepathJson     = "WoodProjectInput.json";
            WoodProjectItem jsonDeserialized = WoodProjectParams.Parse(filepathJson);

            CreateWalls(jsonDeserialized, doc);

            var saveAsOptions = new SaveAsOptions();

            saveAsOptions.OverwriteExistingFile = true;

            ModelPath path = ModelPathUtils.ConvertUserVisiblePathToModelPath("woodproject_result.rvt");

            doc.SaveAs(path, saveAsOptions);
        }
Ejemplo n.º 2
0
        private static void CreateWalls(WoodProjectItem jsonDeserialized, Document newDoc)
        {
            FilteredElementCollector levelCollector = new FilteredElementCollector(newDoc);

            levelCollector.OfClass(typeof(Level));
            var levelElements = levelCollector.ToElements();

            if (levelElements == null || !levelElements.Any())
            {
                throw new InvalidDataException("ElementID is invalid.");
            }

            var defaultWallTypeId = newDoc.GetDefaultElementTypeId(ElementTypeGroup.WallType);

            if (defaultWallTypeId == null || defaultWallTypeId.IntegerValue < 0)
            {
                throw new InvalidDataException("ElementID is invalid.");
            }

            // Find any NP elements in the view
            FilteredElementCollector wallTypeCollector = new FilteredElementCollector(newDoc);

            wallTypeCollector.OfClass(typeof(WallType));
            var wallTypes = wallTypeCollector.ToElements();

            UnitConversionFactors unitFactor = new UnitConversionFactors("cm", "N");

            List <LevelInfo> levelInfos       = new List <LevelInfo>();
            double           currentElevation = 0;

            foreach (var floorItems in jsonDeserialized.Solutions
                     .GroupBy(x => $"Level {x.Floor}")
                     .OrderBy(x => x.Key))
            {
                if (!floorItems.Any())
                {
                    continue;
                }
                var firstFloorItem = floorItems.FirstOrDefault();
                if (firstFloorItem == null)
                {
                    continue;
                }

                var wallHeight = !firstFloorItem.Height.HasValue ||
                                 firstFloorItem.Height < 230 ? 230 :
                                 firstFloorItem.Height > 244 ? 244 :
                                 firstFloorItem.Height.Value;
                wallHeight = wallHeight / unitFactor.LengthRatio;
                var level = new LevelInfo
                {
                    Id        = null,
                    Name      = floorItems.Key,
                    Floor     = int.TryParse(floorItems.Key.Replace("Level ", ""), out var floor) ? floor : 1,
                    Elevator  = currentElevation,
                    Height    = wallHeight,
                    WallInfos = new List <WallInfo>(),
                };

                if (levelElements.Select(x => x.Name).Contains(floorItems.Key))
                {
                    level.Id = levelElements.First(x => x.Name == floorItems.Key).Id;
                }

                level.WallInfos.AddRange(floorItems
                                         .Where(item => !string.IsNullOrWhiteSpace(item.SolutionName))
                                         .Select(item => new WallInfo
                {
                    Curve = Line.CreateBound(
                        new XYZ(item.Sx / unitFactor.LengthRatio, item.Sy / unitFactor.LengthRatio, 0),
                        new XYZ(item.Ex / unitFactor.LengthRatio, item.Ey / unitFactor.LengthRatio, 0)),
                    Solution    = item,
                    TypeId      = wallTypes.FirstOrDefault(x => x.Name == item.SolutionName)?.Id,
                    WallSymbols = floorItems.Where(x =>
                                                   !string.IsNullOrWhiteSpace(x.AssociatedWall) && x.AssociatedWall == item.Id).Select(
                        symbol =>
                        new Symbol
                    {
                        StartPoint = new XYZ(symbol.Sx / unitFactor.LengthRatio,
                                             symbol.Sy / unitFactor.LengthRatio,
                                             symbol.Type == "window"
                                                ? currentElevation + wallHeight / 3
                                                : currentElevation),
                        Type = symbol.Type
                    }).ToList()
                }
                                                 )
                                         );



                levelInfos.Add(level);
                currentElevation += wallHeight;
            }

            using (Transaction constructTrans = new Transaction(newDoc, "Create construct"))
            {
                constructTrans.Start();
                try
                {
                    double floorHeight = 0;
                    foreach (var levelInfo in levelInfos)
                    {
                        var level = CreateLevel(newDoc, levelElements, levelInfo.Elevator, levelInfo);
                        if (level != null)
                        {
                            var area = jsonDeserialized.Areas.FirstOrDefault(x => x.Floor == levelInfo.Floor);
                            if (area != null)
                            {
                                var newFoor = CreateFloor(newDoc, level, area, unitFactor, floorHeight);
                                floorHeight += levelInfo.Height;
                                UpdateWallParam(newFoor, area, Constants.AreaParameterMapping);
                            }
                            foreach (var wallInfo in levelInfo.WallInfos)
                            {
                                var wall = Wall.Create(newDoc, wallInfo.Curve, wallInfo.TypeId ?? defaultWallTypeId,
                                                       level.Id, levelInfo.Height, 0, false, false);
                                UpdateWallParam(wall, wallInfo.Solution, Constants.WallParameterMapping);

                                if (wallInfo.WallSymbols.Any())
                                {
                                    foreach (var wallSymbol in wallInfo.WallSymbols)
                                    {
                                        InsertSymBol(newDoc, wallSymbol.StartPoint, wall, level, wallSymbol.Type);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

                constructTrans.Commit();
            }
        }