CADability dotNET

Here you find code examples for using CADability.

C#: Modifying the background of the ModelView

There is an event before CADability paints the background of a ModelView. With handling this event you can modify the appearance of the ModelView. This sample code shows how to remove the standard coordinate axis display (which always occures at the origin (0,0,0)) and replaces it with a coordinate axis that appears always in the lower left corner of the view. You will have to register this event with each ModelView. The easiest way to to achieve this is to register for the ViewsChangedEvent of the SingleDocumentFrame.

Source Code

CopyC#
void OnPrePaintBackground(IPaintTo3D paintToBackground, ModelView modelView, out ModelView.BackgroungTaskHandled handled)
{
    handled = ModelView.BackgroungTaskHandled.CoordCross; // stop CADability to display the coordinate system

    // some initialisation: black thin lines, no visibility check
    paintToBackground.UseZBuffer(false);
    paintToBackground.SetColor(Color.Black);
    paintToBackground.SetLineWidth(0.0);
    paintToBackground.SetLinePattern(null);

    Projection pr = modelView.Projection;
    double size = pr.DeviceToWorldFactor * 30; // pixel length of axis
    // axis directions in window coordinates
    GeoVector2D xAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.XAxis) - pr.Project(GeoPoint.Origin);
    GeoVector2D yAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.YAxis) - pr.Project(GeoPoint.Origin);
    GeoVector2D zAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.ZAxis) - pr.Project(GeoPoint.Origin);
    // lower left position for the center of the coordinate cross
    int left = modelView.DisplayRectangle.Left + 30;
    int bottom = modelView.DisplayRectangle.Bottom - 30;
    // the four points of the coordinate cross
    PointF p0 = new PointF(left, bottom);
    PointF px = new PointF((float)(left + xAxsis.x), (float)(bottom + xAxsis.y));
    PointF py = new PointF((float)(left + yAxsis.x), (float)(bottom + yAxsis.y));
    PointF pz = new PointF((float)(left + zAxsis.x), (float)(bottom + zAxsis.y));
    // paint in windows coordinates
    paintToBackground.Line2D(p0, px);
    paintToBackground.Line2D(p0, py);
    paintToBackground.Line2D(p0, pz);

    // paint the arrows
    size = pr.DeviceToWorldFactor * 20; // arrow size
    GeoPoint p = pr.WindowToWorld(px);
    GeoVector v = GeoVector.XAxis ^ pr.Direction;
    if (!v.IsNullVector())
    {
        v.Norm();
        PaintTo3D.Arrow1(paintToBackground, p + size / 4 * v, p - size / 4 * v, p + size / 2 * GeoVector.XAxis);
    }
    p = pr.WindowToWorld(py);
    v = GeoVector.YAxis ^ pr.Direction;
    if (!v.IsNullVector())
    {
        v.Norm();
        PaintTo3D.Arrow1(paintToBackground, p + size / 4 * v, p - size / 4 * v, p + size / 2 * GeoVector.YAxis);
    }
    p = pr.WindowToWorld(pz);
    v = GeoVector.ZAxis ^ pr.Direction;
    if (!v.IsNullVector())
    {
        v.Norm();
        PaintTo3D.Arrow1(paintToBackground, p + size / 4 * v, p - size / 4 * v, p + size / 2 * GeoVector.ZAxis);
    }


    // new endpoints as textpositions with larger size (same as above)
    size = pr.DeviceToWorldFactor * 50;
    xAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.XAxis) - pr.Project(GeoPoint.Origin);
    yAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.YAxis) - pr.Project(GeoPoint.Origin);
    zAxsis = pr.Project(GeoPoint.Origin + size * GeoVector.ZAxis) - pr.Project(GeoPoint.Origin);
    px = new PointF((float)(left + xAxsis.x), (float)(bottom + xAxsis.y));
    py = new PointF((float)(left + yAxsis.x), (float)(bottom + yAxsis.y));
    pz = new PointF((float)(left + zAxsis.x), (float)(bottom + zAxsis.y));
    // the letters x, y, z should always appear horizontally, calculate the font directions
    GeoPoint p0w = pr.WindowToWorld(p0);
    GeoPoint pdirxw = pr.WindowToWorld(new PointF(left + 20, bottom));
    GeoPoint pdiryw = pr.WindowToWorld(new PointF(left, bottom - 20));
    GeoVector fontDirx = pdirxw - p0w; // horizontal on the screen
    GeoVector fontDiry = pdiryw - p0w; // vertical on the screen

    // text positions in world coordinates (because there is no Text2D in IPaintTo3D)
    GeoPoint pxw = pr.WindowToWorld(px);
    GeoPoint pyw = pr.WindowToWorld(py);
    GeoPoint pzw = pr.WindowToWorld(pz);

    double fontSize = pr.DeviceToWorldFactor * 20; // font size on world coordinates
    paintToBackground.PrepareText("Arial", "xyz", FontStyle.Regular);
    // paint the letters x,y,z at the end of the prolonged axis
    if (!(GeoVector.XAxis ^ pr.Direction).IsNullVector())
        paintToBackground.Text(fontDirx, fontDiry, pxw, "Arial", "x", FontStyle.Regular, CADability.GeoObject.Text.AlignMode.Center, CADability.GeoObject.Text.LineAlignMode.Center);
    if (!(GeoVector.YAxis ^ pr.Direction).IsNullVector())
        paintToBackground.Text(fontDirx, fontDiry, pyw, "Arial", "y", FontStyle.Regular, CADability.GeoObject.Text.AlignMode.Center, CADability.GeoObject.Text.LineAlignMode.Center);
    if (!(GeoVector.ZAxis ^ pr.Direction).IsNullVector())
        paintToBackground.Text(fontDirx, fontDiry, pzw, "Arial", "z", FontStyle.Regular, CADability.GeoObject.Text.AlignMode.Center, CADability.GeoObject.Text.LineAlignMode.Center);

}