// 创建几何线,无需在事务中 XYZ Point1 = XYZ.Zero; XYZ Point2 = new XYZ(10, 0, 0); XYZ Point3 = new XYZ(10, 10, 0); XYZ Point4 = new XYZ(0, 10, 0); Line geomLine1 = Line.CreateBound(Point1, Point2); Line geomLine2 = Line.CreateBound(Point4, Point3); Line geomLine3 = Line.CreateBound(Point1, Point4); // 几何平面无需开启事务 XYZ origin = XYZ.Zero; XYZ normal = new XYZ(0, 0, 1); Plane geomPlane = Plane.CreateByNormalAndOrigin(normal, origin); // 为了在草图平面绘制模型线,需要开启事务 // 所有的事务都应该包含在 'using' 块中 // 或者在 try-catch-finally 块中进行保护 // 以保证事务不会超出其作用域。 using (Transaction transaction = new Transaction(doc)) { if (transaction.Start("创建模型线") == TransactionStatus.Started) { // 在当前文档中创建草图平面 SketchPlane sketch = SketchPlane.Create(doc,geomPlane); // 使用几何线和草图平面创建模型线 ModelLine line1 = doc.Create.NewModelCurve(geomLine1, sketch) as ModelLine; ModelLine line2 = doc.Create.NewModelCurve(geomLine2, sketch) as ModelLine; ModelLine line3 = doc.Create.NewModelCurve(geomLine3, sketch) as ModelLine; // Ask the end user whether the changes are to be committed or not TaskDialog taskDialog = new TaskDialog("Revit"); taskDialog.MainContent = "点击 [确定] 提交,或 [取消] 回滚事务。"; TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel; taskDialog.CommonButtons = buttons; if (TaskDialogResult.Ok == taskDialog.Show()) { // 由于多种原因,如果事务期间所做的更改未导致有效模型,则事务可能无法提交。 // 如果提交事务失败或被最终用户取消,结果状态将是 RolledBack 而不是 Committed。 // 创建一些几何线。不需要在文档事务中创建。 XYZ Point1 = XYZ.Zero; XYZ Point2 = new XYZ(10, 0, 0); XYZ Point3 = new XYZ(10, 10, 0); XYZ Point4 = new XYZ(0, 10, 0); Line geomLine1 = Line.CreateBound(Point1, Point2); Line geomLine2 = Line.CreateBound(Point4, Point3); Line geomLine3 = Line.CreateBound(Point1, Point4); // 这个几何平面也是事务性的,不需要事务 XYZ origin = XYZ.Zero; XYZ normal = new XYZ(0, 0, 1); Plane geomPlane = Plane.CreateByNormalAndOrigin(normal, origin); // 为了在草图平面绘制模型线,需要开启事务 // 所有的事务都应该包含在 'using' 块中 // 或者在 try-catch-finally 块中进行保护 // 以保证事务不会超出其作用域。 using (Transaction transaction = new Transaction(doc)) { if (transaction.Start("创建模型线") == TransactionStatus.Started) { // 在当前文档中创建草图平面 SketchPlane sketch = SketchPlane.Create(doc, geomPlane); // 使用几何线和草图平面创建 ModelLine 元素 ModelLine line1 = doc.Create.NewModelCurve(geomLine1, sketch) as ModelLine; ModelLine line2 = doc.Create.NewModelCurve(geomLine2, sketch) as ModelLine; ModelLine line3 = doc.Create.NewModelCurve(geomLine3, sketch) as ModelLine; // 询问最终用户是否要提交更改 TaskDialog taskDialog = new TaskDialog("Revit"); taskDialog.MainContent = "点击 [确定] 提交,或 [取消] 回滚事务。"; TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel; taskDialog.CommonButtons = buttons; if (TaskDialogResult.Ok == taskDialog.Show()) { // 由于多种原因,如果事务期间所做的更改未导致有效模型,则事务可能无法提交。 // 如果提交事务失败或被最终用户取消,结果状态将是 RolledBack 而不是 Committed。 if (TransactionStatus.Committed != transaction.Commit()) { TaskDialog.Show("失败", "事务无法提交"); } } else { transaction.RollBack(); } } } return "完成"; if (TransactionStatus.Committed != transaction.Commit()) { TaskDialog.Show("失败", "事务无法提交"); } } else { transaction.RollBack(); } } } return "完成";