|
本帖最后由 Aaronyee 于 2015-11-2 19:13 编辑
求助,我在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
- {
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- 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()
- {
- //add horizonal 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";
- //add vertical 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[i], pts[i + 1]);
- 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[0];
- }
- return null;
- }
- //helper function: convert millimeter to feet
- double mmToFeet(double mmVal)
- {
- return mmVal / 304.8;
- }
- }
- }
复制代码
本站强荐:185娱乐⺁城.足球⺁真_人.彩票齐全⺁手机可投⺁注任何游戏. 首次开户送10元.首存送58元.信誉绝对保证185.cc |
|