CADability dotNET

Here you find code examples for using CADability.

VB: Construct a quader, drill cylindrical or slotted holes

A simple example in VisualBasic. There are four buttons:

Button1 creates a workpiece or an item

Button2 "drills" a hole by removing a cylinder from the workpiece

Button3 makes a slotted hole

Button4 saws the workpiece into two parts.

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

CopyVB.NET
Imports CADability
Imports CADability.Actions
Imports CADability.UserInterface
Imports CADability.GeoObject
Imports CADability.Attribute

''' <summary>
''' A simple example in VisualBasic. There are four buttons: Button1 creates a workpiece or an item, Button2 "drills" a hole by removing
''' a cylinder from the workpiece, Button3 makes a slotted hole, Button4 saws the workpiece into two parts.
''' </summary>
''' <remarks></remarks>
Public Class Form1
    ''' <summary>
    ''' must be in this scope to enable event handling
    ''' </summary>
    ''' <remarks></remarks>
    Private WithEvents soa As SelectObjectsAction
    ''' <summary>
    ''' Implements the Application Exit command
    ''' </summary>
    ''' <param name="MenuId"></param>
    ''' <param name="Processed"></param>
    ''' <remarks></remarks>
    Private Sub CadControl1_ProcessCommandEvent(ByVal MenuId As System.String, ByRef Processed As System.Boolean) Handles CadControl1.ProcessCommandEvent
        If MenuId = "MenuId.App.Exit" Then
            Application.Exit()
            Processed = True
        End If
    End Sub
    ''' <summary>
    ''' Creates the workpiece, which is a simple box
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MakeWorkpiece_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeWorkpiece.Click
        Dim box As Solid
        ' make a box with this fixed dimensions
        box = Make3D.MakeBox(New GeoPoint(0, 0, 0), 400 * GeoVector.XAxis, 50 * GeoVector.YAxis, 20 * GeoVector.ZAxis)
        Dim project As Project
        project = CadControl1.Frame.Project ' access to the current project
        Dim model As Model
        model = project.GetActiveModel() ' access to the current model
        model.RemoveAll() ' make the model empty to only contain the raw workpiece (the box)
        model.Add(box) ' adds the box to the model
        project.SetDefaults(box) ' sets default color and layer etc.
        ColorFrontFace(box) ' make the ront side green
        ' set a view direction so you can see the front side in a isometric view
        CadControl1.Frame.ActiveView.Projection = New Projection(New GeoVector(-1, 1, -1), GeoVector.ZAxis)
        Dim mv As ModelView
        mv = CType(CadControl1.Frame.ActiveView, ModelView)
        mv.ZoomTotal(1.1) ' zoom the workpiece into visibility
    End Sub

    ''' <summary>
    ''' The front plane of the workpiece is set to green
    ''' </summary>
    ''' <param name="sld">The solid to be changed</param>
    ''' <remarks></remarks>
    Private Sub ColorFrontFace(ByVal sld As Solid)
        Dim project As Project
        project = CadControl1.Frame.Project ' acces to the curren project
        ' create or find the color object
        Dim frontcolor As ColorDef = project.ColorList.CreateOrFind("Frontside", Color.Green)

        Dim sh As Shell = sld.Shells(0) ' the only shell of the solid
        ' iterate the faces of the shell
        For i As Integer = 0 To sh.Faces.Length - 1
            Dim face As Face = sh.Faces(i) ' face number i
            If TypeOf (face.Surface) Is PlaneSurface Then ' only check plane surfaces
                Dim pl As PlaneSurface = CType(face.Surface, PlaneSurface)
                If Precision.SameDirection(pl.Normal, GeoVector.YAxis, False) Then ' plane must be parallel to XZPlane
                    If Math.Abs(pl.Plane.Distance(GeoPoint.Origin)) < 0.00001 Then ' plane must contain the origin
                        face.ColorDef = frontcolor ' set the color
                    End If
                End If
            End If
        Next
    End Sub

    Private holeOffset As Double = 25

    ''' <summary>
    ''' Drill a hole into the workpiece. Remove a cylindrical object from the workpiece
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DrillHole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrillHole.Click
        Dim drill As Solid
        drill = Make3D.MakeCylinder(GeoPoint.Origin, 5 * GeoVector.XAxis, 100 * GeoVector.ZAxis)
        'drill is located in the origin and points up (z-direction) 100 mm


        Dim rotate As ModOp
        rotate = ModOp.Rotate(GeoVector.XAxis, Math.PI / 2.0) ' rotation to drill from the front
        drill.Modify(rotate)

        Dim move As ModOp
        move = ModOp.Translate(holeOffset, 25, 10) ' translation to position the drill bit
        drill.Modify(move)

        Remove(drill) 'Remove the drill from the workpiece


        ' some odd machanism to find a position for the next hole, so you can press the button multiple times
        holeOffset = holeOffset + 50
        If holeOffset > 400 Then
            holeOffset = holeOffset - 384
        End If
    End Sub

    ''' <summary>
    ''' Create a slotted hole with the provided values for width, length and height. Actually the solid which is inside the hole
    ''' is created here to later remove this part from the workpiece
    ''' </summary>
    ''' <param name="length"></param>
    ''' <param name="width"></param>
    ''' <param name="height"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SlottedHole(ByVal length As Double, ByVal width As Double, ByVal height As Double) As Solid
        ' onstead of creating two solid cylinders an a box and uniting those three parts (which is much simpler)
        ' we create for demonstation purposes a path as the outline of the slotted hole and extrude it to 
        ' make the(Solid)
        Dim offset = New GeoVector((length - width), 0, 0) ' offset between the centers of the arcs
        Dim a1 As Ellipse = Ellipse.Construct() ' left arc, centered around the origin
        a1.SetArcPlaneCenterRadiusAngles(Plane.XYPlane, GeoPoint.Origin, width / 2.0, Math.PI / 2.0, Math.PI)
        Dim a2 As Ellipse = Ellipse.Construct() ' right arc 
        a2.SetArcPlaneCenterRadiusAngles(Plane.XYPlane, GeoPoint.Origin + offset, width / 2.0, Math.PI / 2.0 * 3.0, Math.PI)
        Dim l1 As Line = Line.Construct() ' lower line
        l1.SetTwoPoints(GeoPoint.Origin - (width / 2.0) * GeoVector.YAxis, GeoPoint.Origin - (width / 2.0) * GeoVector.YAxis + offset)
        Dim l2 As Line = Line.Construct() ' upper line
        l2.SetTwoPoints(GeoPoint.Origin + (width / 2.0) * GeoVector.YAxis, GeoPoint.Origin + (width / 2.0) * GeoVector.YAxis + offset)
        CType(l2, ICurve).Reverse() ' reverse because I was too lazy to exchange the parameters of the above call
        Dim curves(3) As ICurve ' four curves (hence 3)
        curves(0) = a1 ' fill in in the right order
        curves(1) = l1
        curves(2) = a2
        curves(3) = l2
        Dim path As Path = path.Construct()
        Dim ok As Boolean = path.Set(curves) ' make a path with these four curves
        Dim face As Face = Make3D.MakeFace(path, Nothing) ' make a face which fills this path
        Dim res As IGeoObject = Make3D.MakePrism(face, height * GeoVector.ZAxis, Nothing) ' make a prism by extruding the face 
        SlottedHole = CType(res, Solid) ' type cast the result
    End Function

    ''' <summary>
    ''' Make and remove a slotted hole from the workpiece
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MakeSlottedHole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeSlottedHole.Click
        Dim sh As Solid = SlottedHole(50, 12, 20) ' make a slotted hole with these parameters


        Dim move As ModOp
        move = ModOp.Translate(200, 25, 10) ' move the slotted hole to this position
        sh.Modify(move)

        Remove(sh)
    End Sub

    ''' <summary>
    ''' Saw or cut the workpiece into two parts
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Saw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Saw.Click
        ' create a thin plate used as the saw
        Dim saw As Solid = Make3D.MakeBox(GeoPoint.Origin + 300 * GeoVector.XAxis, 3.2 * GeoVector.XAxis, 100 * GeoVector.YAxis, 100 * GeoVector.ZAxis)

        Remove(saw)

    End Sub

    ''' <summary>
    ''' Remove the provided solid from the workpiece(s) already in the model
    ''' </summary>
    ''' <param name="sld"></param>
    ''' <remarks></remarks>
    Private Sub Remove(ByVal sld As Solid)
        Dim project As Project
        project = CadControl1.Frame.Project ' access to the current project
        Dim model As Model
        model = project.GetActiveModel() ' access to the current model
        Dim list As New GeoObjectList ' list to accumulate the remains after removing sld
        For j As Integer = 0 To model.Count - 1
            If TypeOf (model(j)) Is Solid Then
                Dim obj As Solid = CType(model(j), Solid)
                Dim res() As Solid = Make3D.Difference(obj, sld)
                For i As Integer = 0 To res.Length - 1
                    list.Add(res(i))
                Next
            End If
        Next
        model.RemoveAll()
        model.Add(list)
        For Each go As IGeoObject In list
            If TypeOf (go) Is Solid Then
                ColorFrontFace(CType(go, Solid))
            End If
        Next

    End Sub

    Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        If TypeOf (CadControl1.Frame.ActiveAction) Is SelectObjectsAction Then
            soa = CType(CadControl1.Frame.ActiveAction, SelectObjectsAction) ' get access to the select objects action
            ' to be able to react on selections changes (for demonstration only)
        End If
    End Sub

    Private Sub soa_SelectedObjectListChangedEvent(ByVal sender As CADability.Actions.SelectObjectsAction, ByVal selectedObjects As CADability.GeoObject.GeoObjectList) Handles soa.SelectedObjectListChangedEvent
        ' here you could react on the selection change and do something with the newly selected objects
    End Sub

End Class