Creating 3D Software to View AutoCAD® DWG files

In this article, we are going to develop a small 3D Software which can read DWG files which is shown in following video.

Video Demo

Developing 3D Software to View AutoCAD® DWG files

This software can read AutoCAD® Drawing files (DWG), compute volume and weight of 3D object which is being selected. Before start programming, you must have the following tools installed on your machine.

1) Microsoft Visual Studio:

We shall use VB.Net or C# programming languages in Microsoft Visual Studio. You can download Microsoft Visual Studio from :

https://visualstudio.microsoft.com/

Download Visual Studio

2) DevDept Eyeshot:

Eyeshot is a 3D CAD tool based on .NET Framework. It allows programmers to develop 3D CAD functionalities to their software. Eyeshot provides tools for developing 2D / 3D geometry from scratch. It also allows that Geometry can also be imported or exported using other CAD file formats. You can download free version from:

https://www.devdept.com/Products/Eyeshot/Download

Download DevDept Eyeshot

FOLLOW THE STEPS

1) Download and Install Visual Studio.

2) Download and Install Devdept Eyeshot.

3) Open Visual Studio.

4) Click Create a new project.

5) Create a new project
  1. Select Visual Basic
  2. Select Windows Forms App (.Net Framework)
  3. Click Next
6) Configure your new project.
  1. Type Project Name as WindowsApp1
  2. Select Location
  3. Type Solution Name as WindowsApp1
  4. Framework shall be .NET Framework 4.5
  5. Click Create 
7) Adding Reference DLL files.
  1. In Solution Explorer, Right Click on WindowsApp1.
  2. Click Properties in Right Click Menu.
  3. Click References.
  4. Click Add Button.
  5. Click Browse Button.
  6. Go to folder C:\Program Files\devDept Software\Eyeshot Ultimate 12\Bin\x86
  7. Select devDept.Eyeshot.Control.x86.Win.v12.dll
  8. Click Add Button.
  9. Click OK Button.
8) Adding Eyeshot Component in Toolbox.
  1. In Form1.vb [Design], Click on Toolbox.
  2. Right Click on Toolbox Window and Click Choose Items... in Right click menu.
  3. In .Net Framework Components, type dev in Filter Textbox.
  4. Tick mark on Drawings, Model and Simulation.
  5. Click OK to add controls in Toolbox.
9) Adding Model Control in Form1.
  1. In Toolbox, Click Model.
  2. Click in Form1 Window to Add Model1 Control.
  3. In Preset Manager, Select Cosmic and Click Load Button.
10) Adding Other Controls in Form1.
  1. Add ToolStrip in Form1 Window.
  2. Add SplitContainer in Form1 Window.
  3. Add DataGridView in Form1 Window.
11) Adding Other Controls in Form1.
  1. Add ToolStrip in Form1 Window.
  2. Add SplitContainer in Form1 Window.
  3. Add DataGridView in Form1 Window.
12) Add following Program.

Imports devDept.Eyeshot
Imports devDept.Eyeshot.Entities
Imports devDept.Geometry
Imports devDept.Graphics
Imports devDept.Eyeshot.Translators
Public Class Form1
    Dim SelectCheck As Boolean = False
    Dim ent As Object
    Public Sub New()
        InitializeComponent()
    End Sub
    Protected Overrides Sub OnLoad(e As EventArgs)
        Model1.Grid.Visible = False
        Model1.ZoomFit()
        Model1.Invalidate()
        SelectCheck = True
        MyBase.OnLoad(e)
    End Sub
    Private Sub OpenToolStripButton_Click(sender As Object, e As EventArgs) Handles OpenToolStripButton.Click
        OpenFileDialog1.Filter = "DWG file | *.dwg"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = Nothing Then
            Exit Sub
        End If
        Model1.Clear()
        Model1.Invalidate()
        Dim dwgfile As New ReadAutodesk(OpenFileDialog1.FileName, Nothing, Nothing)
        Model1.DoWork(dwgfile)
        dwgfile.AddToScene(Model1)
        Model1.ZoomFit()
        Model1.Invalidate()
    End Sub
    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        ToolStripButton1.CheckOnClick = SelectCheck
        If ToolStripButton1.Checked Then
            Model1.ActionMode = actionType.SelectVisibleByPickDynamic
        Else
            Model1.ActionMode = actionType.None
        End If
    End Sub
    Private Sub Model1_Click(sender As Object, e As EventArgs) Handles Model1.Click
        If ToolStripButton1.Checked = False Then
            Model1.Entities.ClearSelection()
        End If
    End Sub
    Private Sub Model1_SelectionChanged(sender As Object, e As Environment.SelectionChangedEventArgs) Handles Model1.SelectionChanged
        DataGridView1.Rows.Clear()
        For Each ent In Model1.Entities
            Dim count As Int16 = 0
            If ent.selected = True Then
                Dim blockReferenceNotScales As Boolean = True
                Dim vp As New VolumeProperties()
                For i As Integer = 0 To Model1.Entities.Count - 1
                    Dim ent As Entity = Model1.Entities(i)
                    count += AddVolume(vp, ent, blockReferenceNotScales)
                    If Not blockReferenceNotScales Then Exit For
                Next
                Dim x As Double
                Dim y As Double
                Dim z As Double
                Dim xx As Double
                Dim yy As Double
                Dim zz As Double
                Dim xy As Double
                Dim yz As Double
                Dim zx As Double
                Dim world As MomentOfInertia
                Dim centroid As MomentOfInertia
                vp.GetResults(vp.Volume, vp.Centroid, x, y, z, xx, yy, zz, xy, zx, yz, world, centroid)
                DataGridView1.Rows.Add("Volume (m3)", Math.Round(Val(vp.Volume)) / 1000 ^ 3, 5)
                DataGridView1.Rows.Add("Weight (kg)", Math.Round(Val(vp.Volume)) * 7850 / (1000 ^ 3), 5)
            End If
        Next
    End Sub
    Private Function AddVolume(ByVal vp As VolumeProperties, ByVal ent As Entity, ByRef blockReferenceNotScale As Boolean) As Integer
        Dim count As Integer = 0
        blockReferenceNotScale = True
        If ent.Selected Then
            If TypeOf ent Is IFace Then
                Dim itfFace As IFace = CType(ent, IFace)
                Dim meshes As Mesh() = itfFace.GetPolygonMeshes()
                For Each mesh As Mesh In meshes
                    vp.Add(mesh.Vertices, mesh.Triangles)
                Next
                count += 1
            ElseIf TypeOf ent Is BlockReference Then
                Dim br = CType(ent, BlockReference)
                If br.GetScaleFactorX <> 1 AndAlso br.GetScaleFactorY <> 1 AndAlso br.GetScaleFactorZ <> 1 Then
                    blockReferenceNotScale = False
                    Return count
                End If
                For Each e As Entity In br.GetEntities(Model1.Blocks)
                    count += AddVolume(vp, e, blockReferenceNotScale)
                    If Not blockReferenceNotScale Then
                        Return count
                    End If
                Next
            End If
        End If
    End Function
End Class

VIDEO TUTORIAL


#autocad

Comments