|
异常:Modifying is forbidden because the document has no open transaction
我 的代码:
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Manual)]
public class GetExtrusionInProject : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
UIDocument uidoc = uiApp.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = uidoc.Document;
Autodesk.Revit.Creation.Document creationDoc = doc.Create;
//创建族文档
string fileName = @"d:\Users\tianlu\Desktop\公制常规模型.rft";
Autodesk.Revit.DB.Document familyDoc = app.NewFamilyDocument(fileName);
try
{
if (familyDoc.IsFamilyDocument)
{
MessageBox.Show("这是族文档");
CreateExtrusion ce = new CreateExtrusion();
ce._app = app;
ce._familyDoc = familyDoc;
FamilyItemFactory fif = ce._familyDoc.FamilyCreate;
ce.CreateExtrusion1(fif);//创建拉伸实体
}
}
catch (Exception e)
{
message = e.Message;
return Result.Failed;
}
Family newFamily = familyDoc.LoadFamily(doc);
FamilySymbolSet fSymbol = newFamily.Symbols;
foreach (FamilySymbol fs in fSymbol)
{
FamilyInstance fInstance = creationDoc.NewFamilyInstance(new XYZ(0, 0, 0), fs, StructuralType.NonStructural);
}
return Result.Succeeded;
}
}
//创建拉伸实体的类
public class CreateExtrusion
{
public Autodesk.Revit.ApplicationServices.Application _app;
public Autodesk.Revit.DB.Document _familyDoc;
//创建工作平面的函数,输入为平面的原点和法向量
internal SketchPlane CreateSketchPlane(XYZ normal, XYZ origin)
{
Plane geometryPlane = _app.Create.NewPlane(normal, origin);
if (geometryPlane == null)
{
return null;
}
SketchPlane plane = SketchPlane.Create(_familyDoc, geometryPlane);
if (plane == null)
{
return null;
}
return plane;
}
//创建用于拉伸的线
private CurveArrArray CreateExtrusionProfile()
{
CurveArrArray curveArrArray = new CurveArrArray();
CurveArray curveArray1 = new CurveArray();
XYZ p0 = XYZ.Zero;
XYZ p1 = new XYZ(10, 0, 0);
XYZ p2 = new XYZ(10, 10, 0);
XYZ p3 = new XYZ(0, 10, 0);
Line line1 = Line.CreateBound(p0, p1);
Line line2 = Line.CreateBound(p1, p2);
Line line3 = Line.CreateBound(p2, p3);
Line line4 = Line.CreateBound(p3, p0);
curveArray1.Append(line1);
curveArray1.Append(line2);
curveArray1.Append(line3);
curveArray1.Append(line4);
curveArrArray.Append(curveArray1);
return curveArrArray;
}
//创建拉伸实体
public void CreateExtrusion1(FamilyItemFactory familyCreator)
{
CurveArrArray curveArrArray = CreateExtrusionProfile();
SketchPlane sketchPlane = CreateSketchPlane(XYZ.BasisZ, XYZ.Zero);
Extrusion rectExtrusion = familyCreator.NewExtrusion(true, curveArrArray, sketchPlane, 10);
XYZ transPoint1 = new XYZ(-16, 0, 0);
ElementTransformUtils.MoveElement(_familyDoc, rectExtrusion.Id, transPoint1);
}
}
|
|