应用程序:AllViews
Revit平台:所有
Revit版本:2011.0
首次发布用于:9.0
编程语言:C#
技能等级:中等
类别:视图
类型:ExternalCommand
主题:生成包含所有选定视图的新图纸。
相关类:
AllViews.cs
Autodesk.Revit.DB.ViewSet
Autodesk.Revit.Creation.Document.NewViewSheet
项目文件:
AllViews.cs它包含两个类:
-实现接口IExternalCommand的类Command,它是这个外部命令的入口。
-ViewsMgr类负责生成一个新的工作表,其中放置了所有选定的视图。
AllViewsForm.cs它包含类AllViewsForm,它是一个对话框。以下是此对话框的内容:
-树状视图表示可以放置在图纸中的所有视图。
-所有标题栏的列表,用户可以选择其中一个标题栏来创建图纸。
-一个文本框,供用户输入要创建的图纸的名称。
功能:
此示例演示了以下功能:
-查找当前项目中的所有视图和标题栏,并在树状图中显示名称。用户可以选择多个视图。单击“确定”按钮后,将生成一个新图纸,其中放置了所有选定的视图。
-最重要的类是ViewsMgr。它展示了如何从文档中获取所有视图,以及如何通过RevitAPI生成新图纸。
实施:
1.运行此外部命令。
2.为正在生成的图纸选择标题栏。
3.选择要放置在新图纸中的视图。
4.单击“确定”按钮生成新的工作表。
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
AllViews.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.AllViews.CS
{
/// <summary>
/// Implements the Revit add-in interface IExternalCommand
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
#region IExternalCommand Members Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData,
ref string message, Autodesk.Revit.DB.ElementSet elements)
{
Transaction newTran = null;
try
{
if (null == commandData)
{
throw new ArgumentNullException("commandData");
}
Document doc = commandData.Application.ActiveUIDocument.Document;
ViewsMgr view = new ViewsMgr(doc);
newTran = new Transaction(doc);
newTran.Start("AllViews_Sample");
AllViewsForm dlg = new AllViewsForm(view);
if (dlg.ShowDialog() == DialogResult.OK)
{
view.GenerateSheet(doc);
}
newTran.Commit();
return Autodesk.Revit.UI.Result.Succeeded;
}
catch (Exception e)
{
message = e.Message;
if ((newTran != null) && newTran.HasStarted() && !newTran.HasEnded())
newTran.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
#endregion IExternalCommand Members Implementation
}
/// <summary>
/// Generating a new sheet that has all the selected views placed in.
/// </summary>
public class ViewsMgr
{
private TreeNode m_allViewsNames = new TreeNode("Views (all)");
private ViewSet m_allViews = new ViewSet();
private ViewSet m_selectedViews = new ViewSet();
private FamilySymbol m_titleBlock;
private IList<Element> m_allTitleBlocks = new List<Element>();
private ArrayList m_allTitleBlocksNames = new ArrayList();
private string m_sheetName;
private double m_rows;
private double TITLEBAR = 0.2;
private double GOLDENSECTION = 0.618;
/// <summary>
/// Tree node store all views' names.
/// </summary>
public TreeNode AllViewsNames
{
get
{
return m_allViewsNames;
}
}
/// <summary>
/// List of all title blocks' names.
/// </summary>
public ArrayList AllTitleBlocksNames
{
get
{
return m_allTitleBlocksNames;
}
}
/// <summary>
/// The selected sheet's name.
/// </summary>
public string SheetName
{
get
{
return m_sheetName;
}
set
{
m_sheetName = value;
}
}
/// <summary>
/// Constructor of views object.
/// </summary>
/// <param name="doc">the active document</param>
public ViewsMgr(Document doc)
{
GetAllViews(doc);
GetTitleBlocks(doc);
}
/// <summary>
/// Finds all the views in the active document.
/// </summary>
/// <param name="doc">the active document</param>
private void GetAllViews(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
FilteredElementIterator itor = collector.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
itor.Reset();
while (itor.MoveNext())
{
Autodesk.Revit.DB.View view = itor.Current as Autodesk.Revit.DB.View;
// skip view templates because they're invisible in project browser
if (null == view || view.IsTemplate)
{
continue;
}
else
{
ElementType objType = doc.GetElement(view.GetTypeId()) as ElementType;
if (null == objType || objType.Name.Equals("Schedule")
|| objType.Name.Equals("Drawing Sheet"))
{
continue;
}
else
{
m_allViews.Insert(view);
AssortViews(view.Name, objType.Name);
}
}
}
}
/// <summary>
/// Assort all views for tree view displaying.
/// </summary>
/// <param name="view">The view assorting</param>
/// <param name="type">The type of view</param>
private void AssortViews(string view, string type)
{
foreach (TreeNode t in AllViewsNames.Nodes)
{
if (t.Tag.Equals(type))
{
t.Nodes.Add(new TreeNode(view));
return;
}
}
TreeNode categoryNode = new TreeNode(type);
categoryNode.Tag = type;
if (type.Equals("Building Elevation"))
{
categoryNode.Text = "Elevations [" + type + "]";
}
else
{
categoryNode.Text = type + "s";
}
categoryNode.Nodes.Add(new TreeNode(view));
AllViewsNames.Nodes.Add(categoryNode);
}
/// <summary>
/// Retrieve the checked view from tree view.
/// </summary>
public void SelectViews()
{
ArrayList names = new ArrayList();
foreach (TreeNode t in AllViewsNames.Nodes)
{
foreach (TreeNode n in t.Nodes)
{
if (n.Checked && 0 == n.Nodes.Count)
{
names.Add(n.Text);
}
}
}
foreach (Autodesk.Revit.DB.View v in m_allViews)
{
foreach (string s in names)
{
if (s.Equals(v.Name))
{
m_selectedViews.Insert(v);
break;
}
}
}
}
/// <summary>
/// Generate sheet in active document.
/// </summary>
/// <param name="doc">the currently active document</param>
public void GenerateSheet(Document doc)
{
if (null == doc)
{
throw new ArgumentNullException("doc");
}
if (0 == m_selectedViews.Size)
{
throw new InvalidOperationException("No view be selected, generate sheet be canceled.");
}
ViewSheet sheet = ViewSheet.Create(doc, m_titleBlock.Id);
sheet.Name = SheetName;
PlaceViews(m_selectedViews, sheet);
}
/// <summary>
/// Retrieve the title block to be generate by its name.
/// </summary>
/// <param name="name">The title block's name</param>
public void ChooseTitleBlock(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
foreach (FamilySymbol f in m_allTitleBlocks)
{
if (name.Equals(f.Family.Name + ":" + f.Name))
{
m_titleBlock = f;
return;
}
}
}
/// <summary>
/// Retrieve all available title blocks in the currently active document.
/// </summary>
/// <param name="doc">the currently active document</param>
private void GetTitleBlocks(Document doc)
{
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
filteredElementCollector.OfClass(typeof(FamilySymbol));
filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
m_allTitleBlocks = filteredElementCollector.ToElements();
if (0 == m_allTitleBlocks.Count)
{
throw new InvalidOperationException("There is no title block to generate sheet.");
}
foreach (Element element in m_allTitleBlocks)
{
FamilySymbol f = element as FamilySymbol;
AllTitleBlocksNames.Add(f.Family.Name + ":" + f.Name);
if (null == m_titleBlock)
{
m_titleBlock = f;
}
}
}
/// <summary>
/// Place all selected views on this sheet's appropriate location.
/// </summary>
/// <param name="views">all selected views</param>
/// <param name="sheet">all views located sheet</param>
private void PlaceViews(ViewSet views, ViewSheet sheet)
{
double xDistance = 0;
double yDistance = 0;
CalculateDistance(sheet.Outline, views.Size, ref xDistance, ref yDistance);
Autodesk.Revit.DB.UV origin = GetOffSet(sheet.Outline, xDistance, yDistance);
//Autodesk.Revit.DB.UV temp = new Autodesk.Revit.DB.UV (origin.U, origin.V);
double tempU = origin.U;
double tempV = origin.V;
int n = 1;
foreach (Autodesk.Revit.DB.View v in views)
{
Autodesk.Revit.DB.UV location = new Autodesk.Revit.DB.UV(tempU, tempV);
Autodesk.Revit.DB.View view = v;
Rescale(view, xDistance, yDistance);
try
{
//sheet.AddView(view, location);
Viewport.Create(view.Document, sheet.Id, view.Id, new XYZ(location.U, location.V, 0));
}
catch (ArgumentException /*ae*/)
{
throw new InvalidOperationException("The view '" + view.Name +
"' can't be added, it may have already been placed in another sheet.");
}
if (0 != n++ % m_rows)
{
tempU = tempU + xDistance * (1 - TITLEBAR);
}
else
{
tempU = origin.U;
tempV = tempV + yDistance;
}
}
}
/// <summary>
/// Retrieve the appropriate origin.
/// </summary>
/// <param name="bBox"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private Autodesk.Revit.DB.UV GetOffSet(BoundingBoxUV bBox, double x, double y)
{
return new Autodesk.Revit.DB.UV(bBox.Min.U + x * GOLDENSECTION, bBox.Min.V + y * GOLDENSECTION);
}
/// <summary>
/// Calculate the appropriate distance between the views lay on the sheet.
/// </summary>
/// <param name="bBox">The outline of sheet.</param>
/// <param name="amount">Amount of views.</param>
/// <param name="x">Distance in x axis between each view</param>
/// <param name="y">Distance in y axis between each view</param>
private void CalculateDistance(BoundingBoxUV bBox, int amount, ref double x, ref double y)
{
double xLength = (bBox.Max.U - bBox.Min.U) * (1 - TITLEBAR);
double yLength = (bBox.Max.V - bBox.Min.V);
//calculate appropriate rows numbers.
double result = Math.Sqrt(amount);
while (0 < (result - (int)result))
{
amount = amount + 1;
result = Math.Sqrt(amount);
}
m_rows = result;
double area = xLength * yLength / amount;
//calculate appropriate distance between the views.
if (bBox.Max.U > bBox.Max.V)
{
x = Math.Sqrt(area / GOLDENSECTION);
y = GOLDENSECTION * x;
}
else
{
y = Math.Sqrt(area / GOLDENSECTION);
x = GOLDENSECTION * y;
}
}
/// <summary>
/// Rescale the view's Scale value for suitable.
/// </summary>
/// <param name="view">The view to be located on sheet.</param>
/// <param name="x">Distance in x axis between each view</param>
/// <param name="y">Distance in y axis between each view</param>
static private void Rescale(Autodesk.Revit.DB.View view, double x, double y)
{
double Rescale = 2;
Autodesk.Revit.DB.UV outline = new Autodesk.Revit.DB.UV(view.Outline.Max.U - view.Outline.Min.U,
view.Outline.Max.V - view.Outline.Min.V);
if (outline.U > outline.V)
{
Rescale = outline.U / x * Rescale;
}
else
{
Rescale = outline.V / y * Rescale;
}
if (1 != view.Scale && 0 != Rescale)
{
view.Scale = (int)(view.Scale * Rescale);
}
}
}
}
AllViewsForm.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
namespace Revit.SDK.Samples.AllViews.CS
{
partial class AllViewsForm
{
private ViewsMgr m_data;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.allViewsGroupBox = new System.Windows.Forms.GroupBox();
this.allViewsTreeView = new System.Windows.Forms.TreeView();
this.GenerateSheetGroupBox = new System.Windows.Forms.GroupBox();
this.titleBlocksListBox = new System.Windows.Forms.ListBox();
this.sheetNameLabel = new System.Windows.Forms.Label();
this.sheetNameTextBox = new System.Windows.Forms.TextBox();
this.titleBlocksLabel = new System.Windows.Forms.Label();
this.cancelButton = new System.Windows.Forms.Button();
this.oKButton = new System.Windows.Forms.Button();
this.allViewsGroupBox.SuspendLayout();
this.GenerateSheetGroupBox.SuspendLayout();
this.SuspendLayout();
//
// allViewsGroupBox
//
this.allViewsGroupBox.Controls.Add(this.allViewsTreeView);
this.allViewsGroupBox.Location = new System.Drawing.Point(12, 12);
this.allViewsGroupBox.Name = "allViewsGroupBox";
this.allViewsGroupBox.Size = new System.Drawing.Size(212, 242);
this.allViewsGroupBox.TabIndex = 0;
this.allViewsGroupBox.TabStop = false;
this.allViewsGroupBox.Text = "All Views";
//
// allViewsTreeView
//
this.allViewsTreeView.CheckBoxes = true;
this.allViewsTreeView.Location = new System.Drawing.Point(6, 19);
this.allViewsTreeView.Name = "allViewsTreeView";
this.allViewsTreeView.Size = new System.Drawing.Size(200, 217);
this.allViewsTreeView.TabIndex = 0;
this.allViewsTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.allViewsTreeView_AfterCheck);
//
// GenerateSheetGroupBox
//
this.GenerateSheetGroupBox.Controls.Add(this.titleBlocksListBox);
this.GenerateSheetGroupBox.Controls.Add(this.sheetNameLabel);
this.GenerateSheetGroupBox.Controls.Add(this.sheetNameTextBox);
this.GenerateSheetGroupBox.Controls.Add(this.titleBlocksLabel);
this.GenerateSheetGroupBox.Controls.Add(this.cancelButton);
this.GenerateSheetGroupBox.Controls.Add(this.oKButton);
this.GenerateSheetGroupBox.Location = new System.Drawing.Point(267, 12);
this.GenerateSheetGroupBox.Name = "GenerateSheetGroupBox";
this.GenerateSheetGroupBox.Size = new System.Drawing.Size(172, 236);
this.GenerateSheetGroupBox.TabIndex = 1;
this.GenerateSheetGroupBox.TabStop = false;
this.GenerateSheetGroupBox.Text = "Generate Sheet";
//
// titleBlocksListBox
//
this.titleBlocksListBox.FormattingEnabled = true;
this.titleBlocksListBox.Location = new System.Drawing.Point(9, 44);
this.titleBlocksListBox.Name = "titleBlocksListBox";
this.titleBlocksListBox.Size = new System.Drawing.Size(157, 108);
this.titleBlocksListBox.Sorted = true;
this.titleBlocksListBox.TabIndex = 6;
this.titleBlocksListBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.titleBlocksListBox_MouseClick);
//
// sheetNameLabel
//
this.sheetNameLabel.AutoSize = true;
this.sheetNameLabel.Location = new System.Drawing.Point(6, 170);
this.sheetNameLabel.Name = "sheetNameLabel";
this.sheetNameLabel.Size = new System.Drawing.Size(66, 13);
this.sheetNameLabel.TabIndex = 5;
this.sheetNameLabel.Text = "Sheet Name";
//
// sheetNameTextBox
//
this.sheetNameTextBox.Location = new System.Drawing.Point(78, 167);
this.sheetNameTextBox.Name = "sheetNameTextBox";
this.sheetNameTextBox.Size = new System.Drawing.Size(88, 20);
this.sheetNameTextBox.TabIndex = 2;
this.sheetNameTextBox.Text = "Unname";
//
// titleBlocksLabel
//
this.titleBlocksLabel.AutoSize = true;
this.titleBlocksLabel.Location = new System.Drawing.Point(6, 19);
this.titleBlocksLabel.Name = "titleBlocksLabel";
this.titleBlocksLabel.Size = new System.Drawing.Size(59, 13);
this.titleBlocksLabel.TabIndex = 3;
this.titleBlocksLabel.Text = "TitleBlocks";
//
// cancelButton
//
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(91, 207);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(75, 23);
this.cancelButton.TabIndex = 4;
this.cancelButton.Text = "&Cancel";
this.cancelButton.UseVisualStyleBackColor = true;
//
// oKButton
//
this.oKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.oKButton.Location = new System.Drawing.Point(6, 207);
this.oKButton.Name = "oKButton";
this.oKButton.Size = new System.Drawing.Size(75, 23);
this.oKButton.TabIndex = 3;
this.oKButton.Text = "&OK";
this.oKButton.UseVisualStyleBackColor = true;
this.oKButton.Click += new System.EventHandler(this.oKButton_Click);
//
// AllViewsForm
//
this.AcceptButton = this.oKButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(459, 266);
this.Controls.Add(this.GenerateSheetGroupBox);
this.Controls.Add(this.allViewsGroupBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "AllViewsForm";
this.ShowInTaskbar = false;
this.Text = "All Views";
this.Load += new System.EventHandler(this.AllViewsForm_Load);
this.allViewsGroupBox.ResumeLayout(false);
this.GenerateSheetGroupBox.ResumeLayout(false);
this.GenerateSheetGroupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox allViewsGroupBox;
private System.Windows.Forms.GroupBox GenerateSheetGroupBox;
private System.Windows.Forms.Button oKButton;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.TreeView allViewsTreeView;
private System.Windows.Forms.Label titleBlocksLabel;
private System.Windows.Forms.TextBox sheetNameTextBox;
private System.Windows.Forms.Label sheetNameLabel;
private System.Windows.Forms.ListBox titleBlocksListBox;
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598