コード例 #1
0
        private static Floor CreateFloor(Document document, Level level, Area area, UnitConversionFactors unitFactor, double z)
        {
            // Get a floor type for floor creation
            FilteredElementCollector collector = new FilteredElementCollector(document);

            collector.OfClass(typeof(FloorType));
            FloorType floorType = collector.FirstElement() as FloorType;

            CurveArray profile = new CurveArray();

            profile.Append(Line.CreateBound(
                               new XYZ(area.Xmax / unitFactor.LengthRatio, area.Ymax / unitFactor.LengthRatio, z),
                               new XYZ(area.Xmax / unitFactor.LengthRatio, area.Ymin / unitFactor.LengthRatio, z)));
            profile.Append(Line.CreateBound(
                               new XYZ(area.Xmax / unitFactor.LengthRatio, area.Ymin / unitFactor.LengthRatio, z),
                               new XYZ(area.Xmin / unitFactor.LengthRatio, area.Ymin / unitFactor.LengthRatio, z)));
            profile.Append(Line.CreateBound(
                               new XYZ(area.Xmin / unitFactor.LengthRatio, area.Ymin / unitFactor.LengthRatio, z),
                               new XYZ(area.Xmin / unitFactor.LengthRatio, area.Ymax / unitFactor.LengthRatio, z)));
            profile.Append(Line.CreateBound(
                               new XYZ(area.Xmin / unitFactor.LengthRatio, area.Ymax / unitFactor.LengthRatio, z),
                               new XYZ(area.Xmax / unitFactor.LengthRatio, area.Ymax / unitFactor.LengthRatio, z)));

            // The normal vector (0,0,1) that must be perpendicular to the profile.
            XYZ normal = XYZ.BasisZ;

            return(document.Create.NewFloor(profile, floorType, level, false, normal));
        }
コード例 #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();
            }
        }