Hide menu
Loading...
Searching...
No Matches
visualization/wpf/baseviewer/App.xaml.cs

Refer to Basic Viewer Example.

BaseViewer.xaml

<UserControl x:Class="baseviewer.BaseViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cadex="clr-namespace:cadex;assembly=CadExViewWPF"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<Menu x:Name="myMenu" HorizontalAlignment="Stretch" Grid.Row="0">
<MenuItem x:Name="myFileMenuItem" Header="File">
<MenuItem Header="Import" Click="onImport"/>
<Separator />
<MenuItem Header="Quit" Click="onQuit"/>
</MenuItem>
</Menu>
<cadex:ModelPrsWPF_ViewPort x:Name="myViewport" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1">
<TextBox x:Name="myLoadingItem" Text="Loading..." VerticalAlignment="Bottom" HorizontalAlignment="Left" FontSize="20" Visibility="Hidden"/>
</cadex:ModelPrsWPF_ViewPort>
</Grid>
</UserControl>

BaseViewer.xaml.cs

using cadex;
using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
namespace baseviewer
{
public partial class BaseViewer : UserControl
{
public BaseViewer()
{
InitializeComponent();
}
public ModelPrsWPF_ViewPort Viewport
{
get { return myViewport; }
}
public bool loading
{
get { return myLoadingItem.Visibility == Visibility.Visible; }
set { myLoadingItem.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
}
public BaseViewerApplication ViewerApplication
{
get { return myApp; }
set {
myApp = value;
myApp.ErrorMessage += OnErrorMessage;
myApp.LoadingChanged += OnLoadingChanged;
}
}
private void onImport(object sender, System.EventArgs e)
{
var aFilename = ShowFileDialog();
if (aFilename != string.Empty)
{
foreach (Window aWindow in Application.Current.Windows)
{
if (aWindow.IsActive)
{
aWindow.Title = "CAD Exchanger [" + aFilename + "]";
break;
}
}
myApp.Import(aFilename);
}
}
private static string ShowFileDialog()
{
OpenFileDialog anOpenFileDialog = 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 = anOpenFileDialog.ShowDialog();
if (result == true & anOpenFileDialog.OpenFile() != null)
{
return anOpenFileDialog.FileName;
}
}
catch (Exception ex)
{
if (anOpenFileDialog.FileName != String.Empty)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
return string.Empty;
}
private void onQuit(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
private void OnErrorMessage(string theMessage)
{
MessageBox.Show(theMessage);
}
private void OnLoadingChanged(bool theLoading)
{
loading = theLoading;
}
protected BaseViewerApplication myApp;
}
}
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22

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.xaml

<Window x:Class="baseviewer.BaseViewerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:baseviewer="clr-namespace:baseviewer"
mc:Ignorable="d"
Title="CAD Exchanger" Height="600" Width="1000">
<baseviewer:BaseViewer x:Name="myViewer"></baseviewer:BaseViewer>
</Window>

BaseViewerWindow.xaml.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 System.Windows;
namespace baseviewer
{
public partial class BaseViewerWindow : Window
{
public BaseViewerWindow()
{
InitializeComponent();
myViewer.ViewerApplication = new BaseViewerApplication(myViewer);
}
}
}

App.xaml

<Application x:Class="baseviewer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="BaseViewerWindow.xaml">
</Application>

App.xaml.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.Runtime.InteropServices;
using System.Windows;
namespace baseviewer
{
public partial class App : Application
{
// 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);
public App()
{
// 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.");
Shutdown();
}
}
}
}