public void WriteBrepToXml(ObjectId id, string filename, bool append)
{
XmlDocument xmlDoc
= new XmlDocument
(); if (System.IO.File.Exists(filename) && append)
{
xmlDoc.Load(filename);
}
else
{
string s = @"<?xml version='1.0' ?>
<Brep>
</Brep>";
xmlDoc.LoadXml(s);
}
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
string solidType;
if (ent
is Solid3d
) solidType
= "Solide"; else if (ent
is Region
) solidType
= "Region"; else if (ent
is AcDb
.Surface) solidType
= "Surface"; else return;
using (Brep brp
= new Brep
(ent
)) {
XmlElement entNode = xmlDoc.CreateElement(solidType);
entNode.SetAttribute("Handle", ent.Handle.ToString());
xmlDoc.DocumentElement.AppendChild(entNode);
foreach (Complex cplx in brp.Complexes)
{
XmlElement cplxNode = xmlDoc.CreateElement("Complex");
entNode.AppendChild(cplxNode);
foreach (Shell shl in cplx.Shells)
{
XmlElement shlNode = xmlDoc.CreateElement("Shell");
shlNode.SetAttribute("Type", shl.ShellType.ToString());
cplxNode.AppendChild(shlNode);
foreach (AcBr.Face face in shl.Faces)
{
XmlElement faceNode = xmlDoc.CreateElement("Face");
shlNode.AppendChild(faceNode);
try
{
foreach (BoundaryLoop loop in face.Loops)
{
XmlElement loopNode = xmlDoc.CreateElement("Loop");
loopNode.SetAttribute("Type", loop.LoopType.ToString());
faceNode.AppendChild(loopNode);
foreach (Edge edge in loop.Edges)
{
ExternalCurve3d curve = (ExternalCurve3d)edge.Curve;
switch (curve.NativeCurve.GetType().Name)
{
case "LineSegment3d":
LineSegment3d ls = (LineSegment3d)curve.NativeCurve;
XmlElement lsNode = xmlDoc.CreateElement("LineSegment3d");
lsNode.SetAttribute("StartPoint", ls.StartPoint.ToString());
lsNode.SetAttribute("EndPoint", ls.EndPoint.ToString());
lsNode.SetAttribute("Direction", ls.Direction.ToString());
loopNode.AppendChild(lsNode);
break;
case "CircularArc3d":
CircularArc3d ca = (CircularArc3d)curve.NativeCurve;
double angle = Vector3d.XAxis
.TransformBy(Matrix3d.PlaneToWorld(ca.Normal))
.GetAngleTo(ca.ReferenceVector, ca.Normal);
XmlElement caNode = xmlDoc.CreateElement("CircularArc3d");
caNode.SetAttribute("StartPoint", ca.StartPoint.ToString());
caNode.SetAttribute("EndPoint", ca.EndPoint.ToString());
caNode.SetAttribute("Center", ca.Center.ToString());
caNode.SetAttribute("Radius", ca.Radius.ToString());
caNode.SetAttribute("StartAngle", (ca.StartAngle + angle).ToString());
caNode.SetAttribute("EndAngle", (ca.EndAngle + angle).ToString());
caNode.SetAttribute("Normal", ca.Normal.ToString());
loopNode.AppendChild(caNode);
break;
case "EllipticalArc3d":
EllipticalArc3d ea = (EllipticalArc3d)curve.NativeCurve;
XmlElement eaNode = xmlDoc.CreateElement("EllipticalArc3d");
eaNode.SetAttribute("StartPoint", ea.StartPoint.ToString());
eaNode.SetAttribute("EndPoint", ea.EndPoint.ToString());
eaNode.SetAttribute("Center", ea.Center.ToString());
eaNode.SetAttribute("StartAngle", ea.StartAngle.ToString());
eaNode.SetAttribute("EndAngle", ea.EndAngle.ToString());
eaNode.SetAttribute("MajorAxis", ea.MajorAxis.ToString());
eaNode.SetAttribute("MinorAxis", ea.MinorAxis.ToString());
eaNode.SetAttribute("MajorRadius", ea.MajorRadius.ToString());
eaNode.SetAttribute("MinorRadius", ea.MinorRadius.ToString());
eaNode.SetAttribute("Normal", ea.Normal.ToString());
loopNode.AppendChild(eaNode);
break;
case "NurbCurve3d":
XmlElement ncNode = xmlDoc.CreateElement("NurbCurve3d");
NurbCurve3d nc = (NurbCurve3d)curve.NativeCurve;
NurbCurve3dData data = nc.DefinitionData;
ncNode.SetAttribute("StartPoint", nc.StartPoint.ToString());
ncNode.SetAttribute("EndPoint", nc.EndPoint.ToString());
ncNode.SetAttribute("Degree", data.Degree.ToString());
ncNode.SetAttribute("Periodic", data.Periodic.ToString());
ncNode.SetAttribute("Rational", data.Rational.ToString());
for (int i = 0; i < data.Knots.Count; i++)
{
ncNode.SetAttribute(string.Format("Knot_{0}", i + 1), data.Knots[i].ToString());
}
for (int i = 0; i < data.Weights.Count; i++)
{
ncNode.SetAttribute(string.Format("Weight_{0}", i + 1), data.Weights[i].ToString());
}
for (int i = 0; i < data.ControlPoints.Count; i++)
{
ncNode.SetAttribute(string.Format("ControlPoint_{0}", i + 1), data.ControlPoints[i].ToString());
}
loopNode.AppendChild(ncNode);
break;
}
}
}
}
catch
{
XmlElement loopNode = xmlDoc.CreateElement("LoopUnbounded");
faceNode.AppendChild(loopNode);
}
}
}
}
xmlDoc.Save(filename);
}
tr.Commit();
}
}