Here's the sub methods for DrawPartInPicBox();
And here's the sub methods that the above uses, GetEntityPointsToDraw();
class GetEntityPointsToDraw
{
public ArrayList GetEnts(string Strline, int Xpic, int Ypic)
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
ArrayList Pnt3dLst = new ArrayList();
ArrayList colorList = new ArrayList();
string name = "";
string LaNam = "";
using (DocumentLock doclck = doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary GrpDic = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
ObjectId Did = ObjectId.Null;
try
{
Did = GrpDic.GetAt(Strline);
}
catch { }
if (!Did.IsNull)
{
Group EntGrpDic = (Group)trans.GetObject(Did, OpenMode.ForRead);
ObjectId[] Eids = EntGrpDic.GetAllEntityIds();
foreach (ObjectId eid in Eids)
{
string color;
Point3d CnPnt;
double Rad;
double[] Norm;
Point3d[] Smpts;
double inc;
Point3d EnPnt;
Point3d StPnt;
Entity ent = (Entity)trans.GetObject(eid, OpenMode.ForRead);
name = ent.GetType().Name;
LaNam = ent.Layer;
color = SetColorByLayerName(LaNam.ToUpper());
if (color == "")
{
name = "";
}
if (name.Equals("Arc") && LaNam.Length >= 1)
{
AcadArc oLin = (AcadArc)ent.AcadObject;
CnPnt = new Point3d((double[])oLin.Center);
Rad = (double)oLin.Radius;
double StAn = (double)oLin.StartAngle;
double EnAn = (double)oLin.EndAngle;
Norm = (double[])oLin.Normal;
CircularArc3d arc = new CircularArc3d(CnPnt, new Vector3d(Norm), Rad);
arc.SetAngles(StAn, EnAn);
Smpts = arc.GetSamplePoints(12);
Pnt3dLst.Add(Smpts);
colorList.Add(color);
arc.Dispose();
}
if (name.Equals("Polyline") && LaNam.Length >= 1)
{
AcadLWPolyline oLin = (AcadLWPolyline)ent.AcadObject;
Object[] aco = (Object[])oLin.Explode();
AcadEntity[] acEnts = new AcadEntity[aco.Length];
aco.CopyTo(acEnts, 0);
foreach (AcadEntity ace in acEnts)
{
if (ace.ObjectName.Equals("AcDbArc"))
{
AcadArc arc = (AcadArc)ace;
CnPnt = new Point3d((double[])arc.Center);
Rad = (double)arc.Radius;
double StAn = (double)arc.StartAngle;
double EnAn = (double)arc.EndAngle;
Norm = (double[])arc.Normal;
CircularArc3d ARP = new CircularArc3d(CnPnt, new Vector3d(Norm), Rad);
ARP.SetAngles(StAn, EnAn);
Smpts = ARP.GetSamplePoints(12);
Pnt3dLst.Add(Smpts);
colorList.Add(color);
ARP.Dispose();
}
if (ace.ObjectName.Equals("AcDbLine"))
{
AcadLine lin = (AcadLine)ace;
StPnt = new Point3d((double[])lin.StartPoint);
EnPnt = new Point3d((double[])lin.EndPoint);
Smpts = new Point3d[2];
Smpts[0] = StPnt;
Smpts[1] = EnPnt;
Pnt3dLst.Add(Smpts);
colorList.Add(color);
}
}
}
if (name.Equals("Circle") && LaNam.Length >= 1)
{
AcadCircle oLin = (AcadCircle)ent.AcadObject;
CnPnt = new Point3d((double[])oLin.Center);
Rad = (double)oLin.Radius;
Norm = (double[])oLin.Normal;
double perm = (double)oLin.Circumference;
double dist = (perm / 11); //segments of cricle.
inc = dist;
Circle circ = new Circle(CnPnt, new Vector3d(Norm), Rad);
Smpts = new Point3d[12];
for (int x = 0; x < 12; x++)
{
Point3d pt = circ.GetPointAtDist(inc);
Smpts[x] = pt;
inc = inc + dist;
}
Pnt3dLst.Add(Smpts);
colorList.Add(color);
circ.Dispose();
}
if (name.Equals("DBPoint") && LaNam.Length >= 1)
{
AcadPoint oLin = (AcadPoint)ent.AcadObject;
CnPnt = new Point3d((double[])oLin.Coordinates);
Norm = (double[])oLin.Normal;
double distance = 0.1;
double angle = 0.0;
inc = (Math.PI * 0.5);
Smpts = new Point3d[] { PolarPoint(CnPnt, angle, distance), PolarPoint(CnPnt, Math.PI, distance) };
Pnt3dLst.Add(Smpts);
colorList.Add(color);
Smpts = new Point3d[] { PolarPoint(CnPnt, inc, distance), PolarPoint(CnPnt, (Math.PI + inc), distance) };
Pnt3dLst.Add(Smpts);
colorList.Add(color);
}
if (name.Equals("Ellipse") && LaNam.Length >= 1)
{
AcadEllipse oLin = (AcadEllipse)ent.AcadObject;
CnPnt = new Point3d((double[])oLin.Center);
EnPnt = new Point3d((double[])oLin.EndPoint);
StPnt = new Point3d((double[])oLin.StartPoint);
Norm = (double[])oLin.Normal;
Vector3d MaAx = new Vector3d((double[])oLin.MajorAxis);
Vector3d MnAx = new Vector3d((double[])oLin.MinorAxis);
double MaRad = (double)oLin.MajorRadius;
double MnRad = (double)oLin.MinorRadius;
double EnAn = (double)oLin.EndAngle;
double StAn = (double)oLin.StartAngle;
double RR = (double)oLin.RadiusRatio;
EllipticalArc3d arc = new EllipticalArc3d(CnPnt, MaAx, MnAx, MaRad, MnRad, StAn, EnAn);
Smpts = arc.GetSamplePoints(50);
Pnt3dLst.Add(Smpts);
colorList.Add(color);
arc.Dispose();
}
if (name.Equals("Spline") && LaNam.Length >= 1)
{
AcadSpline oLin = (AcadSpline)ent.AcadObject;
Point3dCollection fpts = new Point3dCollection();
Vector3d v1 = new Vector3d((double[])oLin.StartTangent);
Vector3d v2 = new Vector3d((double[])oLin.EndTangent);
for (int x = 0; x < oLin.NumberOfFitPoints; x++)
{
fpts.Add(new Point3d((double[])oLin.GetFitPoint(x)));
}
CubicSplineCurve3d spl = new CubicSplineCurve3d(fpts, v1, v2);
Smpts = spl.GetSamplePoints(50);
Pnt3dLst.Add(Smpts);
colorList.Add(color);
spl.Dispose();
}
if (name.Equals("Line") && LaNam.Length >= 1)
{
AcadLine oLin = (AcadLine)ent.AcadObject;
StPnt = new Point3d((double[])oLin.StartPoint);
EnPnt = new Point3d((double[])oLin.EndPoint);
Smpts = new Point3d[2];
Smpts[0] = StPnt;
Smpts[1] = EnPnt;
Pnt3dLst.Add(Smpts);
colorList.Add(color);
} //if
} //foreach item in group dictionary.
} //ObjectId not null from group dictionary.
} //using transaction.
} //using document lock.
ArrayList NewArray = PointsToPicBox(Pnt3dLst, Xpic, Ypic); //PointF[] Array returned.
NewArray.Add(colorList); //Add color list.
return NewArray;
}
/// <summary>
/// Translates & converts point3d to pointF's
/// </summary>
/// <param name="Pnt3dLst"></param>
/// <param name="Xpic"></param>
/// <param name="Ypic"></param>
/// <returns></returns>
private ArrayList PointsToPicBox(ArrayList Pnt3dLst, int Xpic, int Ypic)
{
ArrayList PntFLst = new ArrayList(); //PointF's for picbox.
Matrix3d MatD = new Matrix3d(); //displacement
Matrix3d MatT = new Matrix3d(); //transformation
Matrix3d MatS = new Matrix3d(); //scaling
Matrix3d MatR = new Matrix3d(); //scaling
Point3d VwDir = (Point3d)AcadApp.GetSystemVariable("VIEWDIR");
Plane pln = new Plane(new Point3d(0, 0, 0), VwDir.GetAsVector().Negate());
MatT = Matrix3d.WorldToPlane(pln);
ArrayList Pnt2dLst = ConvertPtsTo2D(Pnt3dLst, MatT);
PointF min = new Point(1000000, 1000000);
PointF max = new Point(-1000000, -1000000);
foreach (Point2d[] pts in Pnt2dLst) //ArrayList of Point2d's.
{
foreach (Point2d pnt in pts)
{
PointF pt = new PointF((float)pnt.X, (float)pnt.Y);
min = new PointF(pt.X < min.X ? pt.X : min.X, pt.Y < min.Y ? pt.Y : min.Y); //minimum XY point.
max = new PointF(pt.X > max.X ? pt.X : max.X, pt.Y > max.Y ? pt.Y : max.Y); //maximum XY point.
}
}
double VwExtX = max.X - min.X;
double VwExtY = max.Y - min.Y;
double SclFac;
if ((VwExtX * 0.850) >= VwExtY) //X distance of points greater in proportion to Y?
{
SclFac = (Xpic / VwExtX * 0.95); //scale X if greatest length.
}
else
{
SclFac = (Ypic / VwExtY * 0.95); //scale to Y if not X.
}
Point3d p1 = new Point3d(min.X + (VwExtX / 2.0), min.Y + (VwExtY / 2.0), 0); //center of part point.
Point3d p2 = new Point3d(Xpic / 2.0, Ypic / 2.0, 0); //center of picbox.
MatS = Matrix3d.Scaling(SclFac, p2); //scale pointlist to picbox.
MatD = Matrix3d.Displacement(p2.GetVectorTo(p1)); //displacement.
MatR = Matrix3d.Rotation(Math.PI, new Vector3d(0, 0, 1), p1); //rotate.
MatS = MatS * MatD * MatR;
Point3d NewPt = new Point3d();
foreach (Point3d[] pts in Pnt3dLst) //ArrayList of PointF[]'s.
{
PointF[] PntFs = new PointF[pts.Length];
for (int x = 0; x < pts.Length; x++)
{
NewPt = pts[x].TransformBy(MatS * MatT);
PntFs[x] = new PointF((float)NewPt.X, (float)NewPt.Y);
}
PntFLst.Add(PntFs);
}
return PntFLst; //ArrayList of pointF[]'s.
}
private ArrayList ConvertPtsTo2D (ArrayList Pnts, Matrix3d MatToPln)
{
ArrayList Pt2dLst = new ArrayList();
Point3d TempPt = new Point3d();
foreach (Point3d[] pts in Pnts) //ArrayList of Point3d[]'s.
{
Point2d[] Pnts2d = new Point2d[pts.Length];
for (int x = 0; x < pts.Length; x++)
{
TempPt = pts[x].TransformBy(MatToPln);
Pnts2d[x] = new Point2d(TempPt.X, TempPt.Y);
}
Pt2dLst.Add(Pnts2d);
}
return Pt2dLst;
}
private string SetColorByLayerName(string str)
{
string color = "";
switch (str)
{
case "0":
color = "White";
break;
case "BEND":
color = "White";
break;
case "CLEAR":
color = "Green";
break;
case "HID":
color = "Magenta";
break;
case "LDRILL":
color = "Blue";
break;
case "NAIL":
color = "Yellow";
break;
case "OUTSIDE":
color = "Red";
break;
case "PH":
color = "Orange";
break;
default:
color = "";
break;
}
return color;
}
public Point3d PolarPoint(Point3d basepoint, double angle, double distance)
{
return new Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z);
}
Any help appreciated.
Bill