revit二次开发
本帖最后由 Aaronyee 于 2015-11-2 19:13 编辑 <br /><br />求助,我在revit用外部工具运行出现问题,这是我的代码using System;using System.Collections.Generic;
using System.Linq;
using WinForms = System.Windows.Forms;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
namespace chengtai
{
class chengtai:IExternalCommand
{
UIApplication rvtApp;
Document rvtDoc;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet element)
{
rvtApp = commandData.Application ;
rvtDoc = commandData.Application.ActiveUIDocument.Document;
//核实
if (!isRightTemplate(BuiltInCategory.OST_StructuralFoundation))
{
TaskDialog .Show ("Family API","Please open foundation.rft");
return Result.Failed ;
}
//(1.1)新建参考面
addReferencePlanes();
//(1.2)创建拉伸体
Extrusion psolid = createSolid();
return Result.Succeeded ;
}
bool isRightTemplate(BuiltInCategory targetCategory)
{
if (!rvtDoc .IsFamilyDocument )
{
TaskDialog .Show ("Family API","This command works only in the family editor.");
return false ;
}
Family ownerFamily = rvtDoc .OwnerFamily ;
if (ownerFamily == null )
{
TaskDialog .Show ("Family API","This document does not have Owner Family.");
return false ;
}
Category cat = rvtDoc .Settings .Categories .get_Item (BuiltInCategory.OST_StructuralFoundation);
if (!cat .Id .Equals (ownerFamily .FamilyCategory .Id ))
{
TaskDialog .Show ("Family API","The category of this document does not match the context of this command. Please open foundation.rft");
return false ;
}
return true ;
}
void addReferencePlanes()
{
//addhorizonal ref plane
ReferencePlane refFront = findElement(typeof(ReferencePlane), "Front") as ReferencePlane;
XYZ p1 = refFront.BubbleEnd;
XYZ p2 = refFront.FreeEnd;
XYZ pBubbleEnd = new XYZ(p1.X, p1.Y + mmToFeet(1900.0), p1.Z);
XYZ pFreeEnd = new XYZ(p1.X, p1.Y + mmToFeet(1900.0), p1.Z);
XYZ vec = XYZ.BasisZ;
View pViewPlan = findElement(typeof(ViewPlan), "Lower Ref. Level") as View;
ReferencePlane refPlane = rvtDoc.FamilyCreate.NewReferencePlane(pBubbleEnd, pFreeEnd, vec, pViewPlan);
refPlane.Name = "OffsetBack";
pBubbleEnd = new XYZ(p1.X, p1.Y - mmToFeet(1900.0), p1.Z);
pFreeEnd = new XYZ(p1.X, p1.Y - mmToFeet(1900.0), p1.Z);
refPlane = rvtDoc.FamilyCreate.NewReferencePlane(pBubbleEnd, pFreeEnd, vec, pViewPlan);
refPlane.Name = "OffsetFront";
//addvertical ref plane
ReferencePlane refLeft = findElement(typeof(ReferencePlane), "Left") as ReferencePlane;
p1 = refLeft.BubbleEnd;
p2 = refLeft.FreeEnd;
pBubbleEnd = new XYZ(p1.X + mmToFeet (1900.0), p1.Y, p1.Z);
pFreeEnd = new XYZ(p1.X + mmToFeet(1900.0), p1.Y, p1.Z);
refPlane = rvtDoc.FamilyCreate.NewReferencePlane(pBubbleEnd, pFreeEnd, vec, pViewPlan);
refPlane.Name = "OffsetRight";
pBubbleEnd = new XYZ(p1.X - mmToFeet(1900.0), p1.Y, p1.Z);
pFreeEnd = new XYZ(p1.X - mmToFeet(1900.0), p1.Y, p1.Z);
refPlane = rvtDoc.FamilyCreate.NewReferencePlane(pBubbleEnd, pFreeEnd, vec, pViewPlan);
refPlane.Name = "OffsetLeft";
}
Extrusion createSolid()
{
//1. define a profile
CurveArrArray pProfile = createProfile();
//2. create a sketch plane
ReferencePlane pRefPlane = findElement(typeof(ReferencePlane), "ReferencePlane") as ReferencePlane;
SketchPlane pSketchPlane = rvtDoc.FamilyCreate.NewSketchPlane(pRefPlane.Plane);
//3. height of the extrution
double dHeight = mmToFeet(900.0);
//4. create an extrusion here.at this point,just an box,nothing else.
bool bIsSolid = true;
return rvtDoc.FamilyCreate.NewExtrusion(bIsSolid, pProfile, pSketchPlane, dHeight);
}
CurveArrArray createProfile()
{
double w = mmToFeet(3800.0);
double d = mmToFeet(3800.0);
const int nVerts = 4;
XYZ[] pts = new XYZ[] {
new XYZ (-w /2.0, -d /2.0, 0.0),
new XYZ (w /2.0, -d /2.0, 0.0),
new XYZ (w /2.0, d /2.0, 0.0),
new XYZ (-w /2.0, d /2.0, 0.0),
new XYZ (-w /2.0, -d /2.0, 0.0)};
//define a loop
CurveArray pLoop = rvtApp.Application.Create.NewCurveArray();
for (int i = 0; i < nVerts; ++i)
{
Line line = rvtApp.Application.Create.NewLineBound(pts, pts);
pLoop.Append(line);
}
//put the loop in the curveArrArray as a profile
CurveArrArray pProfile = rvtApp.Application.Create.NewCurveArrArray();
pProfile.Append(pLoop);
return pProfile;
}
//helper function: find an element of the given type and the name.
Element findElement(Type targetType, string targetName)
{
FilteredElementCollector collector = new FilteredElementCollector(rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>();
if (elems.Count > 0)
{
return elems;
}
return null;
}
//helper function: convert millimeter to feet
double mmToFeet(double mmVal)
{
return mmVal / 304.8;
}
}
}
www.136088.cc 本站强荐:185娱乐⺁城.足球⺁真_人.彩票齐全⺁手机可投⺁注任何游戏. 首次开户送10元.首存送58元.信誉绝对保证185.cc 本帖最后由 Aaronyee 于 2015-11-2 19:13 编辑 <br /><br />看不懂!
www.136088.cc 本站强荐:185娱乐η城.足球η真_人.彩票齐全η手机可投η注任何游戏. 首次开户送10元.首存送58元.信誉绝对保证185.cc 本帖最后由 Aaronyee 于 2015-11-2 19:13 编辑 <br /><br />你很厉害啊
www.103116.com 威尼斯人:wns185.com首存赠送58元び足球び真_人び各类彩票齐全び提现即时到账 本帖最后由 Aaronyee 于 2015-11-2 19:13 编辑 <br /><br />你很厉害啊
我也是在边看别人写的边学,谈不上厉害,还是初步学习阶段
www.388502.com 皇_冠现_金网:hg88094.com首存送58元.手机可投C注任何游戏满1000送1088彩_金C体育半场结算六_合48倍C各种彩票游戏
页:
[1]