首页/技术分享

二次开发教程:Revit开发通过轴网创建柱子

发布于:2019-08-26 16:45:29
7882人 分享

    [Regeneration(RegenerationOption.Manual)]

    [Transaction(TransactionMode.Manual)]

    public class Class1:IExternalCommand

    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            Document doc = commandData.Application.ActiveUIDocument.Document;

            FilteredElementCollector gridFilter = new FilteredElementCollector(doc);


            // 获取所有的轴网

            List<Grid> allGrids = gridFilter.OfClass(typeof(Grid)).Cast<Grid>().ToList();


            //获取轴网的所有交点

            List<XYZ> Points = new List<XYZ>();

            foreach (Grid grid in allGrids)

            {

                Grid currentGrid = grid;

                foreach (Grid grd in allGrids)

                { 

                    IntersectionResultArray ira = null;

                    SetComparisonResult scr = currentGrid.Curve.Intersect(grd.Curve, out ira);

                    if (ira != null)

                    {

                        IntersectionResult ir = ira.get_Item(0);



                        // 判断点是否重复

                        if (!CheckPoint(Points,ir.XYZPoint))

                        {

                            Points.Add(ir.XYZPoint);

                        }

                    }

                }

            }


            // 设置ViewModel

            MyDataContext myDataContext = new MyDataContext(doc);

            MyWin myWin = new MyWin(myDataContext);

            if (myWin.ShowDialog() ?? false)

            {

                // 返回用户选定的建筑柱FamilySymbol

                FamilySymbol symbol = myDataContext.Symbol as FamilySymbol;


                // 返回柱子的顶部标高

                Level topLevel = myDataContext.TopLevel as Level;


                // 返回柱子的底部标高

                Level btmLevel = myDataContext.BtmLevel as Level;


                // 返回顶部偏移

                double topOffset = myDataContext.TopOffset / 304.8;


                // 返回底部偏移

                double btmOffset = myDataContext.BtmOffset / 304.8;


                //启动 事务

                Transaction trans = new Transaction(doc, "Create");

                trans.Start();

                foreach(XYZ p in Points)

                {

                    FamilyInstance column = doc.Create.NewFamilyInstance(p, symbol, btmLevel, StructuralType.NonStructural);

                    //设置底部偏移

                    column.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_OFFSET_PARAM).Set(btmOffset);

                    //设置顶部标高

                    column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM).Set(topLevel.Id);

                    //设置顶部偏移

                    column.get_Parameter(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM).Set(topOffset);

                }

                // 提交事务

                trans.Commit();

            }



            return Result.Succeeded;

        }


        private bool CheckPoint(List<XYZ> points, XYZ point)

        {

            bool flag = false;

            foreach (XYZ p in points)

            { 

                if(p.IsAlmostEqualTo(point))

                {

                    flag = true;

                    break;

                }

            }

            return flag;

        }

    }




Xaml 代码


<Window x:Class="CreateColumnByGrids.MyWin"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MyWin" SizeToContent="WidthAndHeight">

    <Window.Resources>

        <Style TargetType="Button">

            <Setter Property="Background" Value="White"/>

            <Setter Property="Width" Value="75" />

            <Setter Property="Margin" Value="5"/>

            <Style.Triggers>

                <Trigger Property="IsMouseOver" Value="True">

                    <Setter Property="Background" Value="Red"></Setter>

                </Trigger>

            </Style.Triggers>

        </Style>

        <Style TargetType="TextBox">

            <Setter Property="Width" Value="100"/>

            <Setter Property="VerticalContentAlignment" Value="Center" />

            <Setter Property="Margin" Value="5"/>

        </Style>

        <Style TargetType="Label">

            <Setter Property="Margin" Value="5"/>

            <Setter Property="HorizontalAlignment" Value="Right" />

        </Style>

        <Style TargetType="ComboBox">

            <Setter Property="Margin" Value="5"/>    

            <Setter Property="SelectedIndex" Value="0"/>

        </Style>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto"/>

            <ColumnDefinition Width="Auto"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

        </Grid.RowDefinitions>

        <Label Content="底部标高:" Grid.Row="0" Grid.Column="0"/>

        <ComboBox Grid.Column="1" Grid.Row="0" Name="btmLvl" ItemsSource="{Binding Path=AllLevels}"

                  DisplayMemberPath="Name" SelectedValuePath="Element" SelectedValue="{Binding Path=BtmLevel,UpdateSourceTrigger=PropertyChanged}"/>



        <Label Content="底部偏移:" Grid.Row="1" Grid.Column="0"/>

        <TextBox Grid.Column="1" Grid.Row="1" Name="btmOffset" Text="{Binding Path=BtmOffset, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />



        <Label Content="顶部标高:" Grid.Column="0" Grid.Row="2" />

        <ComboBox Grid.Column="1" Grid.Row="2" Name="topLvl" ItemsSource="{Binding Path=AllLevels}" 

                  DisplayMemberPath="Name" SelectedValuePath="Element" SelectedValue="{Binding Path=TopLevel,UpdateSourceTrigger=PropertyChanged}"/>



        <Label Content="顶部偏移:" Grid.Column="0" Grid.Row="3" />

        <TextBox Grid.Column="1" Grid.Row="3" Name="topOffset" Text="{Binding Path=TopOffset,UpdateSourceTrigger=PropertyChanged}"/>



        <Label Content="柱类型:" Grid.Column="0" Grid.Row="4"/>

        <ComboBox Name="symbol" Grid.Column="1" Grid.Row="4" ItemsSource="{Binding Path=AllSymbol}" DisplayMemberPath="Name" SelectedValuePath="Element"/>

        <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="5" HorizontalAlignment="Right" >

            <Button Name="OK" Content="确定"  Margin="5" Command="{Binding Path= OK_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

            <Button Content="取消"  Margin="5" Command="{Binding Path= Cancel_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}"/>

        </StackPanel>

        

    </Grid>

</Window>




窗体代码:


    public partial class MyWin : Window

    { 

        public MyWin()

        {

            InitializeComponent();           

        }

        public MyWin(MyDataContext dataContext)

        {

            InitializeComponent();             

            this.DataContext = dataContext;

        }

    }


ViewModel代码:


    public class MyDataContext : INotifyPropertyChanged 

    {

        private List<ComboBoxData> _AllLevels = new List<ComboBoxData>();

        public List<ComboBoxData> AllLevels { get { return _AllLevels; } private set { _AllLevels = value; } }



        private List<ComboBoxData> _AllSymbol = new List<ComboBoxData>();

        public List<ComboBoxData> AllSymbol { get { return _AllSymbol; } private set { _AllSymbol = value; } }



        private Element symbol = null;

        public Element Symbol

        {

            get 

            {

                if (symbol == null)

                    return _AllSymbol.First().Element;

                return symbol;

            }

            set 

            {

                symbol = value;

                NotifyPropertyChanged("Symbol");

            }

        }



        private Element topLevel = null;

        public Element TopLevel

        {

            get

            {

                if (topLevel == null)

                    return _AllLevels.First().Element;

                return topLevel;

            }

            set

            {

                topLevel = value;

                NotifyPropertyChanged("TopLevel");

                (OK_Command as OK_Command).NotifyPropertyChanged("OK_Command"); 

            }

        }



        private Element btmLevel = null;

        public Element BtmLevel

        {

            get

            {

                if (btmLevel == null)

                    return _AllLevels.First().Element;

                return btmLevel;

            }

            set

            {

                btmLevel = value; 

                NotifyPropertyChanged("BtmLevel");

                (OK_Command as OK_Command).NotifyPropertyChanged("OK_Command"); 

            }

        }



        private double topOffset = 0.0;

        public double TopOffset

        {

            get { return topOffset; }

            set

            {

                topOffset = value;

                NotifyPropertyChanged("TopOffset");

                (OK_Command as OK_Command).NotifyPropertyChanged("OK_Command");

            }

        }



        private double btmOffset = 0.0;

        public double BtmOffset { get { return btmOffset; } 

            set 

            { 

                btmOffset = value; 

                NotifyPropertyChanged("BtmOffset");

                (OK_Command as OK_Command).NotifyPropertyChanged("OK_Command"); 

            }

        }

        public ICommand OK_Command { get; set; }

        public ICommand Cancel_Command { get; set; }



        public MyDataContext(Document doc)

        {



            // 获取所有的标高

            FilteredElementCollector lvlFilter = new FilteredElementCollector(doc);

            List<Level> lvls = lvlFilter.OfClass(typeof(Level)).Cast<Level>().ToList();

            foreach(Element elm in lvls)

            {

                _AllLevels.Add(new ComboBoxData(elm));

            }



            //获取所有建筑柱的FamilySymbol

            FilteredElementCollector symbolFilter = new FilteredElementCollector(doc);

            List<FamilySymbol> symbols = symbolFilter.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Columns).Cast<FamilySymbol>().ToList();

            foreach (Element elm in symbols)

            {

                _AllSymbol.Add(new ComboBoxData(elm));

            }


            OK_Command = new OK_Command(this);

            Cancel_Command = new Cancel_Command();


        }


        public event PropertyChangedEventHandler PropertyChanged;


        public void NotifyPropertyChanged(string Name)

        {

            if (PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(Name));

            }

        }

    }


Ok 命令:


    public class OK_Command : ICommand

    {

        MyDataContext _context;

        public OK_Command(MyDataContext context)

        {

            _context = context;

        }

        public bool CanExecute(object parameter)

        {

            Level topLevel = _context.TopLevel as Level;

            Level btmLevel = _context.BtmLevel as Level;

            if (topLevel == null || btmLevel == null)

                return false;

            if (topLevel.Elevation + _context.TopOffset - (btmLevel.Elevation + _context.BtmOffset) > 0.001)

                return true;

            return false;

        }



        public event EventHandler CanExecuteChanged;



        public void NotifyPropertyChanged(string Name)

        {

            if (CanExecuteChanged != null)

            {

                CanExecuteChanged(this, new PropertyChangedEventArgs(Name));

            }

        }



        public void Execute(object parameter)

        {

            MyWin myWin = parameter as MyWin;

            if (myWin == null)

                return;



            if (myWin.symbol.SelectedItem == null)

                return;

            if (myWin.topLvl.SelectedItem == null)

                return;

            double TopOffset = 0.0;

            if (!double.TryParse(myWin.topOffset.Text, out TopOffset))

            {

                return;

            }

            if (myWin.btmLvl.SelectedItem == null)

                return;

            double BtmOffset = 0.0;

            if (!double.TryParse(myWin.btmOffset.Text, out BtmOffset))

            {

                return;

            }

            Level TopLevel = myWin.topLvl.SelectedValue as Level;

            Level BtmLevel = myWin.btmLvl.SelectedValue as Level;

            if (TopLevel != null && BtmLevel != null)

            {

                if (BtmLevel.Elevation + BtmOffset > TopLevel.Elevation + TopOffset)

                    return;

            }

            else

            {

                return;

            }



            myWin.DialogResult = true;

            myWin.Close();

        }

    }




Cancel命令:


    public class Cancel_Command : ICommand

    {

        public bool CanExecute(object parameter)

        {          

            return true;

        }


        public event EventHandler CanExecuteChanged;


        public void Execute(object parameter)

        {

            MyWin myWin = parameter as MyWin;

            myWin.DialogResult = false;

            myWin.Close();

        }

    }




Combobox 数据绑定类:


    public class ComboBoxData

    {

        public Element Element { get; set; }

        public string Name { get; set; }

        public ComboBoxData(Element element)

        {

            this.Element = element;

            this.Name = element.Name;

        }

    }

转载请注明来源本文地址:https://www.tuituisoft/blog/3482.html

上一篇:

二次开发教程:Revit开发之放弃重做操作

下一篇:

二次开发教程:Revit开发获取房间内的构件