- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2009-6-20 07:50:00
|
显示全部楼层
本帖最后由 作者 于 2009-6-23 21:11:20 编辑
发现问题在GetParamFromGeCurve方法
Ge的param和DB的param完全不是一回事
要把GeCurve打断再转换为DBCurve应该没问题
贴一个老外的DBCurve转GeCurve类,不完整:)
http://www.theswamp.org/index.php?topic=29217.0[url=http://www.cadtutor.net/forum/showthread.php?t=33523&page=3][/url]- using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- namespace GeometricConversion
- {
- class EntityToGeom
- {
- //Fields
- private CompositeCurve3d m_compCurve;
- private Curve3d[] m_crvArray;
- private Point3d[] m_points;
- private int m_polyVCount;
- private int m_distinctCurves;
- private Boolean m_success;
- private Boolean m_compositable;
- private Curve m_curve;
- private Tolerance m_tol;
- private List<Curve3d> m_listCurve3d;
- //Constructors
- public EntityToGeom()
- {
- InitializeConstructor();
- }
- public EntityToGeom(Curve crv)
- {
- InitializeConstructor();
- m_curve = crv;
- ProcessCrv();
- }
- public EntityToGeom(Curve crv, Tolerance DesiredTolerance)
- {
- InitializeConstructor();
- m_curve = crv;
- m_tol = DesiredTolerance;
- }
- //Destructor
- ~EntityToGeom()
- {
- m_curve.Dispose();
- }
- //Properties
- public Boolean ConversionSuccess
- {
- get
- {
- return m_success;
- }
- }
- public int NumOfPolyVerts
- {
- get
- {
- return m_polyVCount;
- }
- }
- public CompositeCurve3d CurveGeometry
- {
- get
- {
- return m_compCurve;
- }
- }
- public Curve DatabaseCurve
- {
- set
- {
- m_curve = value;
- ProcessCrv();
- }
- }
- public Tolerance DesiredTolerance
- {
- set
- {
- m_tol = DesiredTolerance;
- }
- get
- {
- return m_tol;
- }
- }
- //Internal
- private void InitializeConstructor()
- {
- m_success = false;
- m_compositable = true;
- m_tol = new Tolerance();
- }
- private void ProcessCrv()
- {
- string crvTyp = m_curve.GetType().ToString();
- switch (crvTyp)
- {
- case "Autodesk.AutoCAD.DatabaseServices.Line":
- LineSegment3d ls3d = new LineSegment3d(m_curve.StartPoint, m_curve.EndPoint);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = ls3d;
- m_distinctCurves = 1;
- m_compositable = true;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Ray":
- ProcessConstruction(true);
- m_compositable = false;
- m_distinctCurves = 1;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.XLine":
- ProcessConstruction(false);
- m_compositable = false;
- m_distinctCurves = 1;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Circle":
- ProcessCirculars(false);
- m_distinctCurves = 1;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Arc":
- ProcessCirculars(true);
- m_distinctCurves = 1;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Ellipse":
- ProcessEllipticals();
- m_distinctCurves = 1;
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Polyline":
- ProcessPoly(true);
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Polyline2d":
- ProcessPoly(false);
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Polyline3d":
- ProcessPoly3d();
- break;
- case "Autodesk.AutoCAD.DatabaseServices.Spline":
- ProcessSpline();
- m_distinctCurves = 1;
- break;
- }
- if (m_compositable)
- {
- try
- {
- m_compCurve = new CompositeCurve3d(m_crvArray);
- m_success = true;
- }
- catch
- {
- m_success = false;
- }
- }
- else
- {
- m_success = false;
- }
- }
- private void ProcessPoly(Boolean LightWeight)
- {
- Polyline2d p2d;
- if (LightWeight)
- {
- Polyline p = m_curve as Polyline;
- p2d = p.ConvertTo(false);
- p.Dispose();
- }
- else
- {
- p2d = m_curve as Polyline2d;
- }
- switch (p2d.PolyType)
- {
- case Poly2dType.SimplePoly:
- ProcessSimplePolys(p2d);
- break;
- case Poly2dType.FitCurvePoly:
- ProcessFitPoly(p2d);
- break;
- case Poly2dType.QuadSplinePoly:
- ProcessSplinePoly2d(p2d, 2);
- break;
- case Poly2dType.CubicSplinePoly:
- ProcessSplinePoly2d(p2d, 3);
- break;
- }
- ProcessCurveList();
- }
- private void ProcessPoly3d()
- {
- Polyline3d p3d = m_curve as Polyline3d;
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- PolylineVertex3d pv3d;
- m_polyVCount = Convert.ToInt32(p3d.EndParam);
- m_points = new Point3d[m_polyVCount + 1];
- int i = 0;
- foreach (ObjectId oid in p3d)
- {
- pv3d = trans.GetObject(oid, OpenMode.ForRead) as PolylineVertex3d;
- m_points[i] = pv3d.Position;
- if (i > 0)
- {
- if (!m_points[i].IsEqualTo(m_points[i - 1], m_tol))
- {
- switch (p3d.PolyType)
- {
- case Poly3dType.SimplePoly:
- m_listCurve3d.Add(new LineSegment3d(m_points[i - 1], m_points[i]));
- m_distinctCurves = m_listCurve3d.Count;
- break;
- case Poly3dType.QuadSplinePoly:
- ProcessSplinePoly3d(p3d, 2);
- m_distinctCurves = 1;
- break;
- case Poly3dType.CubicSplinePoly:
- ProcessSplinePoly3d(p3d, 3);
- m_distinctCurves = 1;
- break;
- }
- }
- }
- i++;
- }
- p3d.Dispose();
- }
- ProcessCurveList();
- }
- private void ProcessSpline()
- {
- Spline spl = m_curve as Spline;
- NurbCurve3d nc3d;
- if (spl.HasFitData)
- {
- Vector3dCollection v3c;
- FitData fd = spl.FitData;
- if (fd.TangentsExist)
- {
- v3c = TangentVectorContainer(fd);
- nc3d = new NurbCurve3d(fd.GetFitPoints(), v3c, spl.IsPeriodic);
- }
- else
- {
- nc3d = new NurbCurve3d(fd.GetFitPoints(), new Vector3d(), new Vector3d(), false, false);
- }
- }
- else
- {
- NurbsData nd = spl.NurbsData;
- DoubleCollection dc = nd.GetKnots();
- KnotCollection kc = new KnotCollection();
- foreach (Double d in dc)
- {
- kc.Add(d);
- }
- Point3dCollection p3cCP = nd.GetControlPoints();
- if (nd.Rational)
- {
- DoubleCollection dcw = nd.GetWeights();
- nc3d = new NurbCurve3d(nd.Degree, kc, p3cCP, dcw, nd.Periodic);
- }
- else
- {
- nc3d = new NurbCurve3d(nd.Degree, kc, p3cCP, nd.Periodic);
- }
- }
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = nc3d;
- spl.Dispose();
- }
- private void ProcessConstruction(Boolean StartPoint)
- {
- if (StartPoint)
- {
- Ray r = m_curve as Ray;
- Ray3d r3d = new Ray3d(r.BasePoint, r.SecondPoint);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = r3d;
- r.Dispose();
- }
- else
- {
- Xline x = m_curve as Xline;
- Line3d l3d = new Line3d(x.BasePoint, x.SecondPoint);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = l3d;
- x.Dispose();
- }
- }
- private void ProcessCirculars(Boolean isArc)
- {
- if (isArc)
- {
- Arc a = m_curve as Arc;
- Double param = (a.StartParam + a.EndParam) / 2;
- Point3d p3d = a.GetPointAtParameter(param);
- CircularArc3d ca3 = new CircularArc3d(a.StartPoint, p3d, a.EndPoint);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = ca3;
- a.Dispose();
- }
- else
- {
- Circle c = m_curve as Circle;
- CircularArc3d ca3 = new CircularArc3d(c.Center, c.Normal, c.Radius);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = ca3;
- c.Dispose();
- }
- }
- private void ProcessEllipticals()
- {
- Ellipse e = m_curve as Ellipse;
- EllipticalArc3d ea3 = new EllipticalArc3d(e.Center, e.MajorAxis, e.MinorAxis,
- e.MajorRadius, e.MinorRadius, e.StartParam, e.EndParam);
- m_crvArray = new Curve3d[1];
- m_crvArray[0] = ea3;
- e.Dispose();
- }
- private Vector3dCollection TangentVectorContainer(FitData fd)
- {
- Vector3dCollection v3c = new Vector3dCollection(2);
- v3c.Add(fd.StartTangent);
- v3c.Add(fd.EndTangent);
- return v3c;
- }
- private void ProcessCurveList()
- {
- m_listCurve3d.TrimExcess();
- int i = m_listCurve3d.Count;
- m_crvArray = new Curve3d[i];
- for (int j = 0; j < i; j++)
- {
- m_crvArray[j] = m_listCurve3d[j];
- }
-
- }
- private void ProcessSimplePolys(Polyline2d p2d)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- m_polyVCount = Convert.ToInt32(p2d.EndParam);
- m_points = new Point3d[m_polyVCount + 1];
- m_listCurve3d = new List<Curve3d>();
- int i = 0;
- Double bulge = 0.0;
- Double param = 0.0;
- foreach (Vertex2d v2d in p2d)
- {
- m_points[i] = p2d.VertexPosition(v2d);
- if (i > 0)
- {
- if (!m_points[i].IsEqualTo(m_points[i - 1], m_tol))
- {
- bulge = Math.Round(bulge, 6);
- if (bulge != 0.0)
- {
- param = Convert.ToDouble(i) - 0.5;
- Point3d pt3 = p2d.GetPointAtParameter(param);
- m_listCurve3d.Add(new CircularArc3d(m_points[i - 1], pt3, m_points[i]));
- }
- else
- {
- m_listCurve3d.Add(new LineSegment3d(m_points[i - 1], m_points[i]));
- }
- }
- }
- i++;
- bulge = v2d.Bulge;
- }
- p2d.Dispose();
- }
- }
- private void ProcessFitPoly(Polyline2d p2d)
- {
- int i = 0;
- Double param;
- foreach (Vertex2d v2d in p2d)
- {
- m_points[i] = p2d.VertexPosition(v2d);
- if (i > 0)
- {
- if (!m_points[i].IsEqualTo(m_points[i - 1], m_tol))
- {
- param = Convert.ToDouble(i) - 0.5;
- Point3d pt3 = p2d.GetPointAtParameter(param);
- m_listCurve3d.Add(new CircularArc3d(m_points[i - 1], pt3, m_points[i]));
- }
- }
- i++;
- }
- }
- private void ProcessSplinePoly2d(Polyline2d p3d, int degree)
- {
- }
- private void ProcessSplinePoly3d(Polyline3d p3d, int degree)
- {
- }
- }
- }
|
|