Revit二次开发示例:Journaling

关于Revit Journal读写的例子。

 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion namespace Journaling
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
Transaction tran = new Transaction(commandData.Application.ActiveUIDocument.Document, "Journaling");
tran.Start();
Journaling deal = new Journaling(commandData);
deal.Run();
tran.Commit(); return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
} }
}
}

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Creation = Autodesk.Revit.Creation; namespace Journaling
{
public class Journaling
{
class WallTypeComparer : IComparer<WallType>
{
int IComparer<WallType>.Compare(WallType first, WallType second)
{
return string.Compare(first.Name, second.Name);
}
} ExternalCommandData m_commandData;
bool m_canReadData; XYZ m_startPoint;
XYZ m_endPoint;
Level m_createlevel;
WallType m_createType; List<Level> m_levelList;
List<WallType> m_wallTypeList; public ReadOnlyCollection<Level> Levels
{
get
{
return new ReadOnlyCollection<Level>(m_levelList);
}
} public ReadOnlyCollection<WallType> WallTypes
{
get
{
return new ReadOnlyCollection<WallType>(m_wallTypeList);
}
} public Journaling(ExternalCommandData commandData)
{
m_commandData = commandData;
m_canReadData = (0 < commandData.JournalData.Count) ? true : false; m_levelList = new List<Level>();
m_wallTypeList = new List<WallType>();
InitializeListData();
} public void Run()
{
if (m_canReadData)
{
ReadJournalData();
CreateWall();
}
else
{
if (!DisplayUI())
{
return;
} CreateWall();
WriteJournalData();
}
} public void SetNecessaryData(XYZ startPoint, XYZ endPoint, Level level, WallType type)
{
m_startPoint = startPoint;
m_endPoint = endPoint;
m_createlevel = level;
m_createType = type; } private void InitializeListData()
{
if (null == m_wallTypeList || null == m_levelList)
{
throw new Exception("necessary data members don't initialize.");
} Document document = m_commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
filteredElementCollector.OfClass(typeof(WallType));
m_wallTypeList = filteredElementCollector.Cast<WallType>().ToList<WallType>(); WallTypeComparer comparer = new WallTypeComparer();
m_wallTypeList.Sort(comparer); FilteredElementIterator iter = (new FilteredElementCollector(document)).OfClass(typeof(Level)).GetElementIterator();
iter.Reset();
while (iter.MoveNext())
{
Level level = iter.Current as Level;
if (null == level)
{
continue;
}
m_levelList.Add(level);
} } private void ReadJournalData()
{
Document doc = m_commandData.Application.ActiveUIDocument.Document;
IDictionary<string, string> dataMap = m_commandData.JournalData;
string dataValue = null;
dataValue = GetSpecialData(dataMap, "Wall Type Name");
foreach (WallType type in m_wallTypeList)
{
if (dataValue == type.Name)
{
m_createType = type;
break;
}
}
if (null == m_createType)
{
throw new InvalidDataException("Can't find the wall type from the journal.");
} dataValue = GetSpecialData(dataMap, "Level Id");
ElementId id = new ElementId(Convert.ToInt32(dataValue)); m_createlevel = doc.GetElement(id) as Level;
if (null == m_createlevel)
{
throw new InvalidDataException("Can't find the level from the journal.");
} dataValue = GetSpecialData(dataMap, "Start Point");
m_startPoint = StringToXYZ(dataValue); if (m_startPoint.Equals(m_endPoint))
{
throw new InvalidDataException("Start point is equal to end point.");
}
} private bool DisplayUI()
{
using (JournalingForm displayForm = new JournalingForm(this))
{
displayForm.ShowDialog();
if (DialogResult.OK != displayForm.DialogResult)
{
return false;
}
}
return true;
} private void CreateWall()
{
Creation.Application createApp = m_commandData.Application.Application.Create;
Creation.Document createDoc = m_commandData.Application.ActiveUIDocument.Document.Create; Line geometryLine = Line.CreateBound(m_startPoint, m_endPoint);
if (null == geometryLine)
{
throw new Exception("Create the geometry line failed.");
} Wall createdWall = Wall.Create(m_commandData.Application.ActiveUIDocument.Document,
geometryLine, m_createType.Id, m_createlevel.Id,
15, m_startPoint.Z + m_createlevel.Elevation, true, true);
if (null == createdWall)
{
throw new Exception("Create the wall failed.");
} } private void WriteJournalData()
{
IDictionary<string, string> dataMap = m_commandData.JournalData;
dataMap.Clear(); dataMap.Add("Wall Type Name", m_createType.Name);
dataMap.Add("Level Id", m_createlevel.Id.IntegerValue.ToString());
dataMap.Add("Start Point", XYZToString(m_startPoint));
dataMap.Add("End Point", XYZToString(m_endPoint));
} private static XYZ StringToXYZ(string pointString)
{
double x = 0;
double y = 0;
double z = 0;
string subString; subString = pointString.TrimStart('(');
subString = subString.TrimEnd(')');
string[] coordinateString = subString.Split(',');
if (3 != coordinateString.Length)
{
throw new InvalidDataException("The point iniformation in journal is incorrect");
} try
{
x = Convert.ToDouble(coordinateString[0]);
y = Convert.ToDouble(coordinateString[1]);
z = Convert.ToDouble(coordinateString[2]);
}
catch (Exception)
{
throw new InvalidDataException("The point information in journal is incorrect"); } return new Autodesk.Revit.DB.XYZ(x, y, z);
} private static string XYZToString(XYZ point)
{
string pointString = "(" + point.X.ToString() + "," + point.Y.ToString() + ","
+ point.Z.ToString() + ")";
return pointString;
} private static string GetSpecialData(IDictionary<string, string> dataMap, string key)
{
string dataValue = dataMap[key]; if (string.IsNullOrEmpty(dataValue))
{
throw new Exception(key + "information is not exits in journal.");
}
return dataValue;
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using Autodesk.Revit.DB;
using Autodesk.Revit.UI; namespace Journaling
{
public partial class JournalingForm : System.Windows.Forms.Form
{
const double Precision = 0.00001;
Journaling m_dataBuffer; public JournalingForm(Journaling dataBuffer)
{
InitializeComponent(); m_dataBuffer = dataBuffer; typeComboBox.DataSource = m_dataBuffer.WallTypes;
typeComboBox.DisplayMember = "Name";
levelComboBox.DataSource = m_dataBuffer.Levels;
levelComboBox.DisplayMember = "Name";
} private void okButton_Click(object sender, EventArgs e)
{ XYZ startPoint = new XYZ(Convert.ToDouble(textBox1.Text.Trim()),
Convert.ToDouble(textBox2.Text.Trim()),
Convert.ToDouble(textBox3.Text.Trim())); XYZ endPoint = new XYZ(Convert.ToDouble(textBox4.Text.Trim()),
Convert.ToDouble(textBox5.Text.Trim()),
Convert.ToDouble(textBox6.Text.Trim())); if (startPoint.Equals(endPoint))
{
TaskDialog.Show("Revit", "Start point should not equal end point.");
return;
} double diff = Math.Abs(startPoint.Z - endPoint.Z);
if (diff > Precision)
{
TaskDialog.Show("Revit", "Z coordinate of start and end points should be equal.");
return;
} Level level = levelComboBox.SelectedItem as Level; // level information
if (null == level) // assert it isn't null
{
TaskDialog.Show("Revit", "The selected level is null or incorrect.");
return;
} WallType type = typeComboBox.SelectedItem as WallType; // wall type
if (null == type) // assert it isn't null
{
TaskDialog.Show("Revit", "The selected wall type is null or incorrect.");
return;
} // Invoke SetNecessaryData method to set the collected support data
m_dataBuffer.SetNecessaryData(startPoint, endPoint, level, type); // Set result information and close the form
this.DialogResult = DialogResult.OK;
this.Close();
} private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
} }
}
上一篇:Flash图表控件FusionCharts调整图表百分比大小


下一篇:Spring RMI (Spring 远程方法调用)