下載app免費領取會員
[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)
{
InterpResultArray ira = null;
SetComparisonResult scr = currentGrid.Curve.Intersect(grd.Curve, out ira);
if (ira != null)
{
InterpResult 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;
}
}
本文版權歸腿腿教學網及原創(chuàng)作者所有,未經授權,謝絕轉載。
上一篇:二次開發(fā)教程:Revit開發(fā)之放棄重做操作