在Revit里可以把一部分构件组合起来创建为一个部件,
然后可以创建一些针对这个部件的视图
通过API来创建主要用到两个类
AssemblyInstance
AssemblyViewUtils
下面是一个小例子:
Command:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
IList<Reference> refs = uidoc.Selection.PickObjects(ObjectType.Element);
List<Element> elems = new List<Element>();
List<ElementId> ids = new List<ElementId>();
foreach (Reference r in refs)
{
Element e = doc.GetElement(r);
elems.Add(e);
ids.Add(e.Id);
}
ViewModel myViewModel = new ViewModel(elems);
AssemblyViewWindow myWin = new AssemblyViewWindow(myViewModel);
if(myWin.ShowDialog()??false)
{
TransactionGroup transGrop = new TransactionGroup(doc, "transGroup");
transGrop.Start();
Transaction trans = new Transaction(doc, "trans");
trans.Start();
AssemblyInstance assemblyInstance = AssemblyInstance.Create(doc, ids, myViewModel.Category.Id);
trans.Commit();
trans = new Transaction(doc, "rename");//
trans.Start();
ElementId typeId = assemblyInstance.GetTypeId();
Element type = doc.GetElement(typeId);
type.Name = myViewModel.AssemblyInstanceName;
View3D view3d = AssemblyViewUtils.Create3DOrthographic(doc, assemblyInstance.Id);
trans.Commit();
transGrop.Assimilate();
}
return Result.Succeeded;
}
Window:
<Window x:Class="AssemblyView.AssemblyViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="新建部件" SizeToContent="WidthAndHeight">
<Window.Resources>
<Style TargetType="Label">
<Setter Property="Margin" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Margin" Value="5"/>
<Setter Property="Width" Value="80"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Margin" Value="5"/>
<Setter Property="Width" Value="80"/>
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="Width" Value="75"/>
<Setter Property="Height" Value="23"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="类型名称:" Grid.Column="0" Grid.Row="0"/>
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=AssemblyInstanceName, UpdateSourceTrigger=PropertyChanged}"/>
<Label Content="命名类别:" Grid.Column="0" Grid.Row="1"/>
<ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Path=Categories, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=Category, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"/>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right">
<Button Content="确定" IsEnabled="{Binding Path=IsEnabled, UpdateSourceTrigger=PropertyChanged}" Command="{Binding Path=OK_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"/>
<Button Content="取消" Command="{Binding Path=Cancel_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor ,AncestorType=Window}}" />
</StackPanel>
</Grid>
</Window>
public partial class AssemblyViewWindow : Window
{
public AssemblyViewWindow()
{
InitializeComponent();
}
public AssemblyViewWindow(ViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}
ViewModel:
public class ViewModel:INotifyPropertyChanged
{
private string assemblyInstanceName = string.Empty;
public string AssemblyInstanceName
{
get
{
return assemblyInstanceName;
}
set
{
assemblyInstanceName = value;
NotifyPropertyChanged("AssemblyInstanceName");
CheckOkBtn();
}
}
private List<Category> categories = new List<Category>();
public List<Category> Categories
{
get
{
return categories;
}
}
private Category category = null;
public Category Category
{
get
{
return category;
}
set
{
category = value;
NotifyPropertyChanged("Category");
CheckOkBtn();
}
}
private bool isEnabled = false;
public bool IsEnabled
{
get { return isEnabled; }
set
{
isEnabled = value;
}
}
private OK_Command ok_Command = null;
public OK_Command OK_Command
{
get { return ok_Command; }
}
private Cancel_Command cancel_Command = null;
public Cancel_Command Cancel_Command
{
get { return cancel_Command; }
}
public ViewModel(List<Element> elems)
{
foreach(Element elm in elems)
{
if (categories.Where(m=>m.Name==elm.Category.Name).Count()==0)
categories.Add(elm.Category);
}
this.cancel_Command = new Cancel_Command();
this.ok_Command = new OK_Command();
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private void CheckOkBtn()
{
if (assemblyInstanceName == string.Empty || category == null)
IsEnabled = false;
else
IsEnabled = true;
NotifyPropertyChanged("IsEnabled");
}
}
ICommand:
public class OK_Command : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
AssemblyViewWindow myWin = parameter as AssemblyViewWindow;
myWin.DialogResult = true;
myWin.Close();
}
}
public class Cancel_Command : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
AssemblyViewWindow myWin = parameter as AssemblyViewWindow;
myWin.DialogResult = false;
myWin.Close();
}
}
最新文章
政策新闻 | 芜湖市研究中心公用处联合党支部召开党员大会开展深入贯彻中央八项规定精神学习教育专项理论学习、《习近平关于加强党的作风建设论述摘编》交流研讨会 政策新闻 | 贵阳市设质站:服务零距离│高效办成一件事 前置服务和技术辅导多措并举促进施工图审查“拿证即开工” 政策新闻 | 南充市住建局关于四川盛晖源建筑工程有限公司等17家建筑企业资质申请审查意见的公示 政策新闻 | 湖南省住建厅关于核准长沙城发恒通工程建设有限公司等企业27项建筑业企业资质和143项建筑业企业资质换证后延续的公告(2025年第14批) 政策新闻 | 高温错峰战凌晨 汉江大桥焕新亮夜空 政策新闻 | 重大项目建设丨守信中路道路工程建设项目稳步推进 政策新闻 | 广西住建厅关于公布2025年第G23批建筑业企业资质(特殊行业)审查结果的通告 政策新闻 | 宜宾市南溪区综合行政执法局:推进油烟治理 守护清新空气 政策新闻 | 关于公开征集宁波市住房和城乡建设“十五五”规划意见建议的公告 政策新闻 | 丹东关于印发《全市大跨度体育场馆和公共演出活动场所安全专项排查整治工作方案》的通知