Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been working on a macros that creates a solid from a CAD file:
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
StringBuilder stb = new StringBuilder();
Element dwg=doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element));
Options opt= new Options();
opt.ComputeReferences = true;
opt.IncludeNonVisibleObjects=false;
opt.View = doc.ActiveView;
GeometryElement geo = dwg.get_Geometry(opt);
//Tuple<int,List<XYZ>> myTuple = new Tuple<int, List<XYZ>()>() ;
List<Tuple<int,IList<XYZ>>> myTuple = new List<Tuple<int, IList<XYZ>>>();
foreach (GeometryObject g in geo) {
if (g as GeometryInstance != null) {
GeometryInstance geoInst=g as GeometryInstance;
foreach (GeometryObject obj in geoInst.GetInstanceGeometry()) {
if (obj as PolyLine != null) {
PolyLine pln=obj as PolyLine;
IList<XYZ> coordinates= pln.GetCoordinates();
myTuple.Add(new Tuple<int,IList<XYZ>> ( pln.Id,coordinates) );
}
}
}
}
myTuple= myTuple.OrderBy(X=>X.Item1).ToList();
SolidOptions solidOpt = new SolidOptions(ElementId.InvalidElementId,ElementId.InvalidElementId);
Transaction tr = new Transaction(doc,"solid");
tr.Start();
// for (int i = 0; i < myTuple.Count -1 ; i++) {
//
// if (HermiteSpline.Create(myTuple[i].Item2,false).IsClosed == true && HermiteSpline.Create(myTuple[i+1].Item2,false).IsClosed == true) {
//// stb.AppendLine("closed");
//// CurveLoop cl1 = new CurveLoop();
//// cl1.Append(HermiteSpline.Create(myTuple[i].Item2,false));
////
//// CurveLoop cl2 = new CurveLoop();
//// cl2.Append(HermiteSpline.Create(myTuple[i+1].Item2,false));
////
//// Solid loft = GeometryCreationUtilities.CreateBlendGeometry(cl1,cl2,null);
//
//
// }
// CurveLoop cl1 = new CurveLoop();
// cl1.Append(HermiteSpline.Create(myTuple[i].Item2,false));
//
// CurveLoop cl2 = new CurveLoop();
// cl2.Append(HermiteSpline.Create(myTuple[i+1].Item2,false));
//
// Solid loft = GeometryCreationUtilities.CreateBlendGeometry(cl1,cl2,null);
// }
for (int i = 0; i < myTuple.Count -1; i++) {
List<CurveLoop> profile = new List<CurveLoop>();
CurveLoop cl1=new CurveLoop();
CurveLoop cl2=new CurveLoop();
cl1.Append(HermiteSpline.Create(myTuple[i].Item2,false));
cl2.Append(HermiteSpline.Create(myTuple[i+1].Item2,false));
profile.Add(cl1);
profile.Add(cl2);
Solid loft = GeometryCreationUtilities.CreateLoftGeometry(profile,solidOpt);
}Sometimes it Keeps telling me there are opened curves which in turns is not true as when I picked the bend top and bottom face, I see a closed boundary.
Other times it just fails. I've been struggling using createBlendSolid and Loft solid. Even direct shape form:
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
StringBuilder stb = new StringBuilder();
Element dwg=doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element));
Options opt= new Options();
opt.ComputeReferences = true;
opt.IncludeNonVisibleObjects=false;
opt.View = doc.ActiveView;
GeometryElement geo = dwg.get_Geometry(opt);
int count=0;
Dictionary<int,IList<XYZ>> dictionary = new Dictionary<int, IList<XYZ>>();
foreach (GeometryObject g in geo) {
if (g as GeometryInstance != null) {
GeometryInstance geoInst=g as GeometryInstance;
foreach (GeometryObject obj in geoInst.GetInstanceGeometry()) {
if (obj as PolyLine != null) {
PolyLine pln=obj as PolyLine;
IList<XYZ> coordinates= pln.GetCoordinates();
dictionary.Add(count, coordinates);
count += 1;
}
}
}
}
Transaction tr = new Transaction(doc,"solid - afry");
tr.Start();
IList<Element> materials = new FilteredElementCollector(doc).OfClass(typeof(Material)).ToElements().ToList();
Material mat = null;
foreach (Material mate in materials) {
if (mate.Name == "Concrete, Cast In Situ") {
mat=mate;
}
}
TessellatedShapeBuilder builder= new TessellatedShapeBuilder();
builder.OpenConnectedFaceSet(true);
foreach (KeyValuePair<int,IList<XYZ>> pair in dictionary) {
//stb.AppendLine(pair.Key.ToString());
IList<XYZ> outerLoopVertices = new List<XYZ>();
for (int i = 0; i < pair.Value.Count - 2; i++) {
outerLoopVertices.Add(pair.Value[i]);
outerLoopVertices.Add(pair.Value[i+1]);
outerLoopVertices.Add(pair.Value[i+2]);
}
// TessellatedFace tesseFace= new TessellatedFace(pair.Value,mat.Id);
// if (builder.DoesFaceHaveEnoughLoopsAndVertices(tesseFace)) {
// builder.AddFace(tesseFace);
// //stb.AppendLine(tesseFace.ToString());
// //stb.AppendLine(tesseFace.GetBoundaryLoops().ToString());
// }
TessellatedFace tesseFace= new TessellatedFace(outerLoopVertices,mat.Id);
if (builder.DoesFaceHaveEnoughLoopsAndVertices(tesseFace)) {
builder.AddFace(tesseFace);
}
}
builder.CloseConnectedFaceSet();
builder.Target = TessellatedShapeBuilderTarget.Solid;
builder.Fallback=TessellatedShapeBuilderFallback.Abort;
builder.Build();
TessellatedShapeBuilderResult result = builder.GetBuildResult();
ElementId categoryId= new ElementId(BuiltInCategory.OST_GenericModel);
DirectShape ds=DirectShape.CreateElement(doc,categoryId);
ds.SetShape(result.GetGeometricalObjects());
ds.Name="Presa";
tr.Commit();
What we expect:
We have a bunch of DWG data and it takes a lot to manually creating blend components. Any suggestions on how to improve my code or find the issue of this matter?
Best regards, Miguel G
Solved! Go to Solution.
Developer Advocacy and Support +