150 lines
4.9 KiB
C#
150 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.DB.Architecture;
|
|
using Autodesk.Revit.UI;
|
|
using Autodesk.Revit.UI.Selection;
|
|
|
|
namespace GeologyToolkit
|
|
{
|
|
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
|
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
|
|
public class DirectShapeSurface
|
|
{
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
UIApplication uiapp = commandData.Application;
|
|
UIDocument uidoc = uiapp.ActiveUIDocument;
|
|
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
|
|
Document doc = uidoc.Document;
|
|
DocumentSet docset = uiapp.Application.Documents;
|
|
|
|
#region FilterExecute
|
|
|
|
var walls = new FilteredElementCollector(doc).OfClass(typeof(Wall)).ToElements();
|
|
using (Transaction trans = new Transaction(doc, "DirectShape"))
|
|
{
|
|
try
|
|
{
|
|
trans.Start();
|
|
var rd = new Random();
|
|
var x = rd.Next(10, 100);
|
|
var y = rd.Next(10, 100);
|
|
var z = rd.Next(10, 20);
|
|
List<XYZ> points = new List<XYZ>();
|
|
points.Add(new XYZ(0, 0, 0));
|
|
points.Add(new XYZ(0, 100, 0));
|
|
points.Add(new XYZ(100, 100, 0));
|
|
points.Add(new XYZ(100, 0, 0));
|
|
List<PolymeshFacet> facets = new List<PolymeshFacet>();
|
|
|
|
#if REVIT2018 || REVIT2019
|
|
|
|
#else
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
x += i * x;
|
|
y += i * y;
|
|
z += i * z;
|
|
PolymeshFacet facet = new PolymeshFacet(x, y, z);
|
|
facets.Add(facet);
|
|
}
|
|
if (TopographySurface.IsValidFaceSet(facets, points))
|
|
{
|
|
TopographySurface surface = TopographySurface.Create(doc, points, facets);
|
|
}
|
|
#endif
|
|
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.RollBack();
|
|
}
|
|
|
|
return Result.Failed;
|
|
}
|
|
}
|
|
|
|
#endregion FilterExecute
|
|
|
|
#region RepeatExecute
|
|
|
|
bool isCoutine = true;
|
|
using (Transaction trans = new Transaction(doc, "default"))
|
|
{
|
|
try
|
|
{
|
|
while (isCoutine)
|
|
{
|
|
try
|
|
{
|
|
trans.Start();
|
|
//do something.
|
|
trans.Commit();
|
|
}
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
|
|
{
|
|
trans.Commit();
|
|
return Result.Succeeded;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.RollBack();
|
|
}
|
|
|
|
return Result.Failed;
|
|
}
|
|
}
|
|
|
|
#endregion RepeatExecute
|
|
|
|
#region SelectExecute
|
|
|
|
using (Transaction trans = new Transaction(doc, "default"))
|
|
{
|
|
try
|
|
{
|
|
Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择XXX");
|
|
Element e = uidoc.Document.GetElement(refer);
|
|
|
|
trans.Start();
|
|
//Do Something.
|
|
trans.Commit();
|
|
}
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
|
|
{
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.RollBack();
|
|
}
|
|
|
|
return Result.Cancelled;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.RollBack();
|
|
}
|
|
|
|
return Result.Failed;
|
|
}
|
|
}
|
|
|
|
#endregion SelectExecute
|
|
|
|
return Result.Succeeded;
|
|
}
|
|
}
|
|
} |