コード例 #1
0
ファイル: CmdFamily.cs プロジェクト: liujun155/RevitDemo
        /// <summary>
        /// 按配置放置族
        /// </summary>
        /// <param name="ent"></param>
        public void SetConfig(SetConfigEnt ent)
        {
            if (_selFamily == null)
            {
                return;
            }
            try
            {
                Family family = FindAndLoadFamily(_doc, _selFamily);
                //创建族实例
                FamilySymbol     fSymbol = null;
                ISet <ElementId> idSet   = family.GetFamilySymbolIds();
                if (idSet.Count > 0)
                {
                    fSymbol = _doc.GetElement(idSet.ElementAt <ElementId>(0)) as FamilySymbol;
                    if (!fSymbol.IsActive)
                    {
                        fSymbol.Activate();
                    }
                    //获取实例参数
                    FamilyInstance instance = _doc.Create.NewFamilyInstance(new XYZ(0, 0, 0), fSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                    string         len      = instance.LookupParameter("长度").AsValueString();
                    string         wid      = instance.LookupParameter("宽度").AsValueString();
                    string         hei      = instance.LookupParameter("高度").AsValueString();
                    double         length   = double.Parse(len);
                    double         width    = double.Parse(wid);
                    double         height   = double.Parse(hei);
                    _doc.Delete(instance.Id);

                    RoomTag        rmtag          = FindRoomTag(_doc, ent.RoomName);
                    Room           room           = rmtag.Room;
                    BoundingBoxXYZ boundingBoxXYZ = room.get_BoundingBox(_doc.ActiveView);
                    XYZ            minPoint       = boundingBoxXYZ.Min;
                    XYZ            maxPoint       = boundingBoxXYZ.Max;
                    XYZ            minp           = AUnit.FT2MM(minPoint);
                    XYZ            maxp           = AUnit.FT2MM(maxPoint);
                    var            roomLength     = maxp.X - minp.X; //房间长度
                    var            roomWidth      = maxp.Y - minp.Y; //房间宽度
                    double         lenCount       = Math.Floor(roomLength / length);
                    double         widCount       = Math.Floor(roomWidth / width);
                    if (ent.Num > lenCount * widCount)
                    {
                        TaskDialog.Show("提示", "当前房间仅能摆放此族" + lenCount * widCount + "个");
                        return;
                    }
                    if (ent.Direction == "横向")
                    {
                        double allCount = Math.Ceiling(ent.Num / lenCount);
                        for (int i = 1; i <= allCount; i++)
                        {
                            double y = 0;
                            if (i == 1)
                            {
                                y = width / 2;
                            }
                            else
                            {
                                y = width * i - width / 2;
                            }
                            for (int j = 1; j <= lenCount; j++)
                            {
                                if (i * j > ent.Num)
                                {
                                    break;
                                }
                                double x = 0;
                                if (j == 1)
                                {
                                    x = length / 2;
                                }
                                else
                                {
                                    x = length * j - length / 2;
                                }
                                XYZ point = minp + new XYZ(x, y, 0);
                                point = AUnit.MM2FT(point);
                                //创建族实例
                                FamilyInstance fi = _doc.Create.NewFamilyInstance(point, fSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                            }
                        }
                    }
                    else if (ent.Direction == "纵向")
                    {
                        double allCount = Math.Ceiling(ent.Num / widCount);
                        for (int i = 1; i <= allCount; i++)
                        {
                            double x = 0;
                            if (i == 1)
                            {
                                x = length / 2;
                            }
                            else
                            {
                                x = length * i - length / 2;
                            }
                            for (int j = 1; j <= widCount; j++)
                            {
                                if (i * j > ent.Num)
                                {
                                    break;
                                }
                                double y = 0;
                                if (j == 1)
                                {
                                    y = width / 2;
                                }
                                else
                                {
                                    y = width * j - width / 2;
                                }
                                XYZ point = minp + new XYZ(x, y, 0);
                                point = AUnit.MM2FT(point);
                                //创建族实例
                                FamilyInstance fi = _doc.Create.NewFamilyInstance(point, fSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", ex.Message);
                return;
            }
            TaskDialog.Show("提示", "创建成功");
        }