Hide menu
Loading...
Searching...
No Matches
visualization/winforms/baseviewer/Program.cs

Refer to Basic Viewer Example.

BaseViewer.cs

using cadex;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace baseviewer
{
public partial class BaseViewer : Form
{
public BaseViewer()
{
InitializeComponent();
}
public BaseViewerApplication ViewerApplication
{
get { return myApp; }
set {
myApp = value;
myApp.ErrorMessage += OnErrorMessage;
myApp.LoadingChanged += OnLoadingChanged;
}
}
public ModelPrsWinForms_ViewPort Viewport
{
get { return myViewPort; }
}
public void OnLoadingChanged(bool theLoading)
{
myLoadingItem.Visible = theLoading;
}
private void OnErrorMessage(string theMessage)
{
MessageBox.Show(theMessage);
}
private void OnQuit (object sender, EventArgs es)
{
Application.Exit();
}
private void OnImport(object sender, EventArgs es)
{
var aFilename = ShowFileDialog();
if (aFilename != string.Empty)
{
Parent.Text = "CAD Exchanger [" + aFilename + "]";
myApp.Import(aFilename);
}
}
private static string ShowFileDialog()
{
OpenFileDialog aFileDialog = new OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
CheckFileExists = true,
CheckPathExists = true,
Filter = "STEP Files (*.step)|*.stp",
FilterIndex = 2,
RestoreDirectory = true,
ShowReadOnly = true
};
try
{
var result = aFileDialog.ShowDialog();
if (result == DialogResult.OK & aFileDialog.OpenFile() != null)
{
return aFileDialog.FileName;
}
}
catch (Exception ex)
{
if (aFileDialog.FileName != string.Empty)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
return string.Empty;
}
protected BaseViewerApplication myApp;
protected ModelPrsWinForms_ViewPort myViewPort;
}
}
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22

BaseViewer.Designer.cs

using System.Windows.Forms;
using System.Drawing;
using cadex;
namespace baseviewer
{
partial class BaseViewer
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.SuspendLayout();
this.TopLevel = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Size = new System.Drawing.Size(1000, 600);
//
// myMenu
//
myMenu = new MenuStrip();
myMenu.Dock = DockStyle.Fill;
var aFileMenu = new ToolStripMenuItem("&File");
myMenu.Items.Add(aFileMenu);
var anImportMenuItem = new ToolStripMenuItem("&Import");
anImportMenuItem.Click += OnImport;
aFileMenu.DropDownItems.Add(anImportMenuItem);
var anExitMenuItem = new ToolStripMenuItem("&Quit");
anExitMenuItem.Click += OnQuit;
aFileMenu.DropDownItems.Add(anExitMenuItem);
//
// myViewPort
//
myViewPort = new ModelPrsWinForms_ViewPort();
myViewPort.Dock = DockStyle.Fill;
//
// aLayout
//
TableLayoutPanel aLayout = new TableLayoutPanel();
aLayout.RowStyles.Clear();
aLayout.ColumnStyles.Clear();
aLayout.RowCount += 2;
aLayout.ColumnCount += 1;
aLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F));
aLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, myMenu.Height));
aLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
aLayout.Controls.Add(myMenu, 0, 0);
aLayout.Controls.Add(myViewPort, 0, 1);
aLayout.Dock = DockStyle.Fill;
//
// myLoadingItem
//
myLoadingItem = new System.Windows.Forms.Label();
myLoadingItem.Text = "Loading...";
myLoadingItem.TextAlign = ContentAlignment.MiddleCenter;
myLoadingItem.Font = new Font(Font.Name, 13, FontStyle.Regular);
myLoadingItem.Size = new Size(myLoadingItem.PreferredWidth, myLoadingItem.PreferredHeight);
myLoadingItem.MaximumSize = myLoadingItem.Size;
myLoadingItem.BackColor = Color.White;
myLoadingItem.Visible = false;
myLoadingItem.Dock = DockStyle.Bottom;
//
// BaseViewer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(myLoadingItem);
this.Controls.Add(aLayout);
this.MainMenuStrip = this.myMenu;
this.Name = "BaseViewer";
this.Text = "CAD Exchanger";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip myMenu;
private System.Windows.Forms.Label myLoadingItem;
}
}

BaseViewerApplication.cs

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2023, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
using cadex;
using System.ComponentModel;
namespace baseviewer
{
public class BaseViewerApplication
{
public delegate void LoadingDelegate(bool theLoading);
public delegate void ErrorMessageDelegate(string theMessage);
public event LoadingDelegate LoadingChanged;
public event ErrorMessageDelegate ErrorMessage;
public BaseViewerApplication(BaseViewer theViewer)
{
myViewer = theViewer;
myViewer.Viewport.AttachToScene(myScene);
myScene.AddRoot(myRoot);
myWorker.DoWork += OnDoWork;
myWorker.RunWorkerCompleted += OnImportCompleted;
}
public bool IsBusy
{
get { return myWorker.IsBusy; }
}
public void Import(string theFilename)
{
if (IsBusy)
{
return;
}
Clear();
myWorker.RunWorkerAsync(theFilename);
LoadingChanged (true);
}
private void OnDoWork(object sender, DoWorkEventArgs e)
{
var aFilename = e.Argument as string;
Read(aFilename);
DisplayModel();
e.Result = true;
}
private void Read(string theFilename)
{
aReader.SetReaderParameters(myReaderParameters);
if (!aReader.Read(new Base_UTF16String(theFilename), myModel))
{
return;
}
}
private void DisplayModel()
{
CreateSceneNodes();
myScene.Update();
myScene.Wait();
}
private void Clear()
{
myRoot.RemoveChildrenNodes();
myScene.Update();
myModel.Clear();
}
protected virtual void CreateSceneNodes()
{
ModelPrs_SceneNodeFactory aFactory = new ModelPrs_SceneNodeFactory();
var aRoot = aFactory.CreateGraph(myModel, ModelData_RepresentationMask.ModelData_RM_BRep);
myRoot.AddChildNode(aRoot);
}
private void OnImportCompleted(object theSender, RunWorkerCompletedEventArgs theEvent)
{
if (!theEvent.Cancelled && !(bool)theEvent.Result)
{
ErrorMessage(theEvent.Error.Message);
}
LoadingChanged(false);
myViewer.Viewport.AnimatedFitAll();
}
protected ModelPrs_SceneNode myRoot = new ModelPrs_SceneNode();
protected ModelData_Model myModel = new ModelData_Model();
protected ModelPrs_Scene myScene = new ModelPrs_Scene();
protected STEP_ReaderParameters myReaderParameters = new STEP_ReaderParameters();
protected BaseViewer myViewer;
private BackgroundWorker myWorker = new BackgroundWorker();
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
void SetReaderParameters(const Base_ReaderParameters &theParameters)
Sets reader parameters.
Definition: ModelData_ModelReader.cxx:216
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:182
Defines parameters of the STEP_Reader.
Definition: STEP_ReaderParameters.hxx:27
ModelData_RepresentationMask
Defines a mask to filter part representations.
Definition: ModelData_RepresentationMask.hxx:25

BaseViewerWindow.cs

using cadex;
using System;
using System.Windows.Forms;
namespace baseviewer
{
public partial class BaseViewerWindow : Form
{
public BaseViewerWindow()
{
InitializeComponent();
myViewer.ViewerApplication = new BaseViewerApplication(myViewer);
myViewer.Show();
}
}
}

BaseViewerWindow.Designer.cs

using System.Windows.Forms;
using System.Drawing;
using cadex;
namespace baseviewer
{
partial class BaseViewerWindow
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.SuspendLayout();
this.Text = "CAD Exchanger";
this.IsMdiContainer = false;
this.ClientSize = new System.Drawing.Size(1000, 600);
//
// myViewer
//
myViewer = new BaseViewer();
myViewer.ClientSize = this.ClientSize;
myViewer.Dock = DockStyle.Fill;
//
// BaseViewerWindow
//
this.Controls.Add(myViewer);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private BaseViewer myViewer;
}
}

Program.cs

using cadex;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace baseviewer
{
static class Program
{
// For more information see https://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
[STAThread]
static void Main()
{
// Add runtime path to CAD Exchanger libraries (e.g. use libraries compiled with Visual Studio 2015)
SetDllDirectory("../../../../../../../win64/vc14.1/bin");
string aKey = LicenseKey.Value();
// Activate the license (aKey must be defined in cadex_license.cs)
if (!LicenseManager.Activate(aKey))
{
MessageBox.Show("Failed to activate CAD Exchanger license.");
Application.Exit();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new BaseViewerWindow());
}
}
}