Revit Intro Lab2 - Hello World
이번 Lab2는 Lab1의 연속적인 프로그램으로 기존에 시작한 프로젝트에 파일을 추가하여 진행하시면 됩니다.
기존의 프로젝트를 오픈하여 새로운 클래스를 정의할 새로운 파일을 추가한다.
파일명 : 2_DbElement.cs 클래스명 : DBElement
필요한 네임스페이스
System;
Autodesk.Revit.ApplicationServices;
Autodesk.Revit.Attributes;
Autodesk.Revit.DB;
Autodesk.Revit.UI;
Autodesk.Revit.UI.Selection;
주요변수의 설정
uiApp = commandData.Application;
_app = commandData.Application.Application;
uiDoc = commandData.Application.ActiveUIDocument;
_doc = commandData.Application.ActiveUIDocument.Document
목적 : 이 실험에서는 개체가 레빗에서 어떻게 표현되는지 알압고 객체의 정보는 찾는 방법에 대해 알수 있습니다.
객체의 기본정보 확인하기
객체의 특성세트 찾기
객체의 특정한 특성 찾기
위치정보 찾기
형태정보 찾기
1. Pick an Element
2. Basic Element Information
3. Identify Element
4. Parameters
5. Location Information
6. Geometry Information
ShowBasicElementInfo(e);
IdentifyElement(e);
ShowParameters(e, "Element Parameters: ");
ShowParameters(elemType, "Type Parameters: ");
RetrieveParameter(e, "Element Parameter (by Name and BuiltInParameter): ");
RetrieveParameter(elemType, "Type Parameter (by Name and BuiltInParameter): ");
ShowLocation(e);
ShowGeometry(e);
C# 소스
// 코딩에 적용될 네임스페이스를 선언한다
using System;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
// 네임스페이스 명칭을 IntroCs로 선언한다
namespace IntroCs
{
[Transaction(TransactionMode.Automatic)]
public class DBElement : IExternalCommand
{
Application _app;
Document _doc;
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet element)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
_app = uiApp.Application;
_doc = uiDoc.Document;
Reference r = uiDoc.Selection.PickObject(ObjectType.Element, "Pick an element");
Element e = uiDoc.Document.GetElement(r);
ShowBasicElementInfo(e);
IdentifyElement(e);
ShowParameters(e, "Element Parameters: ");
ElementId elemTypeId = e.GetTypeId();
ElementType elemType = (ElementType)_doc.GetElement(elemTypeId);
ShowParameters(elemType, "Type Parameters: ");
RetrieveParameter(e, "Element Parameter (by Name and BuiltInParameter): ");
RetrieveParameter(elemType, "Type Parameter (by Name and BuiltInParameter): ");
ShowLocation(e);
ShowGeometry(e);
return Result.Succeeded;
}
// ShowBasicElementInfo 서브루틴
public void ShowBasicElementInfo(Element e)
{
string s = "You picked:"
+ "\r\nClass name = " + e.GetType().Name
+ "\r\nCategory = " + e.Category.Name
+ "\r\nElement id = " + e.Id.ToString();
ElementId elemTypeId = e.GetTypeId();
ElementType elemType = (ElementType)_doc.GetElement(elemTypeId);
s += "\r\nIts ElementType:"
+ " Class name = " + elemType.GetType().Name
+ " Category = " + elemType.Category.Name
+ " Element type id = " + elemType.Id.ToString();
TaskDialog.Show("Basic Element Info", s);
}
// IdentifyElement 서브루틴
public void IdentifyElement(Element e)
{
string s = "";
if (e is Wall)
{
s = "Wall";
}
else if (e is Floor)
{
s = "Floor";
}
else if (e is RoofBase)
{
s = "Roof";
}
else if (e is FamilyInstance)
{
if (e.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Doors)
{
s = "Door";
}
else if (e.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Windows)
{
s = "Window";
}
else if (e.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Furniture)
{
s = "Furniture";
}
else
{
s = "Component family instance";
}
}
else if (e is HostObject)
{
s = "System family instance";
}
else
{
s = "Other";
}
s = "You have picked: " + s;
TaskDialog.Show("Identify Element", s);
}
// ShowParameters 서브루틴
public void ShowParameters(Element e, string header)
{
string s = string.Empty;
foreach (Parameter param in e.Parameters)
{
string name = param.Definition.Name;
string val = ParameterToString(param);
s += "\r\n" + name + " = " + val;
}
TaskDialog.Show(header, s);
}
// ParameterToString 서브루틴
public static string ParameterToString(Parameter param)
{
string val = "none";
if (param == null)
{
return val;
}
switch (param.StorageType)
{
case StorageType.Double:
double dVal = param.AsDouble();
val = dVal.ToString();
break;
case StorageType.Integer:
int iVal = param.AsInteger();
val = iVal.ToString();
break;
case StorageType.String:
string sVal = param.AsString();
val = sVal;
break;
case StorageType.ElementId:
ElementId idVal = param.AsElementId();
val = idVal.IntegerValue.ToString();
break;
case StorageType.None:
break;
}
return val;
}
// RetrieveParameter 서브루틴
public void RetrieveParameter(Element e, string header)
{
string s = string.Empty;
Parameter param = e.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
if (param != null)
{
s += "Comments (by BuiltInParameter) = " + ParameterToString(param) + "\n";
}
param = e.get_Parameter("Mark");
if (param != null)
{
s += "Mark (by Name) = " + ParameterToString(param) + "\n";
}
param = e.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
if (param != null)
{
s += "Type Comments (by BuiltInParameter) = " + ParameterToString(param) + "\n";
}
param = e.get_Parameter("Fire Rating");
if (param != null)
{
s += "Fire Rating (by Name) = " + ParameterToString(param) + "\n";
}
param = e.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM);
if (param != null)
{
s += "SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM (only by BuiltInParameter) = "
+ ParameterToString(param) + "\n";
}
param = e.get_Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM);
if (param != null)
{
s += "SYMBOL_FAMILY_NAME_PARAM (only by BuiltInParameter) = "
+ ParameterToString(param) + "\n";
}
TaskDialog.Show(header, s);
}
// ShowLocation 서브루틴
public void ShowLocation(Element e)
{
string s = "Location Information: " + "\n" + "\n";
Location loc = e.Location;
if (loc is LocationPoint)
{
LocationPoint locPoint = (LocationPoint)loc;
XYZ pt = locPoint.Point;
double r = locPoint.Rotation;
s += "LocationPoint" + "\n";
s += "Point = " + PointToString(pt) + "\n";
s += "Rotation = " + r.ToString() + "\n";
}
else if (loc is LocationCurve)
{
LocationCurve locCurve = (LocationCurve)loc;
Curve crv = locCurve.Curve;
s += "LocationCurve" + "\n";
s += "EndPoint(0)/Start Point = " + PointToString(crv.GetEndPoint(0)) + "\n";
s += "EndPoint(1)/End point = " + PointToString(crv.GetEndPoint(1)) + "\n";
s += "Length = " + crv.Length.ToString() + "\n";
s += "JoinType(0) = " + locCurve.get_JoinType(0).ToString() + "\n";
s += "JoinType(1) = " + locCurve.get_JoinType(1).ToString() + "\n";
}
TaskDialog.Show("Show Location", s);
}
public static string PointToString(XYZ p)
{
if (p == null)
{
return "";
}
return string.Format("({0},{1},{2})",
p.X.ToString("F2"), p.Y.ToString("F2"),
p.Z.ToString("F2"));
}
// ShowGeometry 서브루틴
public void ShowGeometry(Element e)
{
Options opt = _app.Create.NewGeometryOptions();
opt.DetailLevel = ViewDetailLevel.Fine;
GeometryElement geomElem = e.get_Geometry(opt);
string s = (geomElem == null) ?
"no data" :
GeometryElementToString(geomElem);
TaskDialog.Show("Show Geometry", s);
}
// GeometryElementToString 서브루틴
public static string GeometryElementToString(GeometryElement geomElem)
{
string str = string.Empty;
foreach (GeometryObject geomObj in geomElem)
{
if (geomObj is Solid)
{
Solid solid = (Solid)geomObj;
str += "Solid" + "\n";
}
else if (geomObj is GeometryInstance)
{
str += " -- Geometry.Instance -- " + "\n";
GeometryInstance geomInstance = (GeometryInstance)geomObj;
GeometryElement geoElem = geomInstance.SymbolGeometry;
str += GeometryElementToString(geoElem);
}
else if (geomObj is Curve)
{
Curve curv = (Curve)geomObj;
str += "Curve" + "\n";
}
else if (geomObj is Mesh)
{
Mesh mesh = (Mesh)geomObj;
str += "Mesh" + "\n";
}
else
{
str += " *** unkown geometry type" + geomObj.GetType().ToString();
}
}
return str;
}
}
}
Add-in Manifest File
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<!-- 2_DbElement -->
<AddIn Type="Command">
<Text>DB Element</Text>
<FullClassName>IntroCs.DBElement</FullClassName>
<Assembly>H:\Visual Studio 2010\Projects\IntroCs\IntroCs\bin\Debug\IntroCs.dll</Assembly>
<AddInId>4aa1c59b-2b3c-433f-86a5-b527aa7fb442</AddInId>
<VendorId>ADNP</VendorId>
<VendorDescription>Autodesk, Inc. www.autodesk.com</VendorDescription>
</AddIn>
</RevitAddIns>
'Revit > Revit API' 카테고리의 다른 글
[레빗 API 시작하기] Revit Intro Lab4 - Element Modification (0) | 2013.12.05 |
---|---|
[레빗 API 시작하기] Revit Intro Lab3 - Element Filtering (2) | 2013.11.18 |
C# 시작하기 (0) | 2013.10.19 |
[레빗 API 시작하기] Lab1-HelloWorld (0) | 2013.10.13 |
AutoCAD .NET Developer's Guide (0) | 2012.08.16 |