Hide menu
Loading...
Searching...
No Matches
visualization/qtquick_qml/drawingviewer/main.cxx

Refer to Drawing Viewer Example

DrawingViewerApplication.hxx

// ****************************************************************************
// $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.
//
// ****************************************************************************
#ifndef __DrawingViewerApplication_HeaderFile
#define __DrawingViewerApplication_HeaderFile
#include "../baseviewer/BaseViewerApplication.hxx"
class DrawingViewerApplication : public BaseViewerApplication
{
Q_OBJECT
public:
DrawingViewerApplication();
void CreateSceneNodes() override;
signals:
void sceneNodeCreated (const QStringList& theSheetNamesList);
private slots:
void onInitialized();
void onCurrentSheetIndexChanged (int thePreviousIndex, int theCurrentIndex);
void onSceneNodeCreated (const QStringList& theSheetNamesList);
private:
void Clear() override;
std::vector<cadex::ModelPrs_SceneNode> myDrawingSheetNodes;
};
#endif // __DrawingViewerApplication_HeaderFile

DrawingViewerApplication.cxx

// ****************************************************************************
// $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.
//
// ****************************************************************************
#define __CADEX_PREVIEW_DRAWINGS 1
#include "DrawingViewerApplication.hxx"
#include <cadex/ModelPrs_SceneNodeFactory.hxx>
#include <cadex/ModelPrsQtQuick_ViewPort.hxx>
#include <cadex/ModelData_Direction.hxx>
#include <cadex/ModelData_Drawing.hxx>
#include <cadex/ModelData_DrawingSheet.hxx>
#include <cadex/ModelData_Point.hxx>
#include <cadex/DWG_ReaderParameters.hxx>
#include <QtQuick/QQuickWindow>
using namespace cadex;
DrawingViewerApplication::DrawingViewerApplication()
{
DWG_ReaderParameters aDWGReaderParameters;
aDWGReaderParameters.ReadDrawing() = true;
myReaderParameters = aDWGReaderParameters;
myRoot.SetSelectionMode (ModelPrs_SM_Node);
connect (this, &BaseViewerApplication::initialized, this, &DrawingViewerApplication::onInitialized);
connect (this, &DrawingViewerApplication::sceneNodeCreated, this, &DrawingViewerApplication::onSceneNodeCreated);
}
void DrawingViewerApplication::CreateSceneNodes()
{
auto aDrawing = myModel.Drawing();
if (!aDrawing) {
ShowMessageBox ("The imported model doesn't contain drawing entities.<br>Please import another model.");
return;
}
QStringList aSheetNamesList;
if (aDrawing.NumberOfSheets() > 1) {
aSheetNamesList.push_back ("All");
}
size_t aSheetIndex = 0;
size_t aModelSheetIndex = 0;
ModelData_Drawing::SheetIterator aSheetIt (aDrawing);
while (aSheetIt.HasNext()) {
const auto& aSheet = aSheetIt.Next();
aSheetIndex++;
auto aSheetIndexStr = QString::number (aSheetIndex);
QString aSheetSourceName (reinterpret_cast<const QChar*> (aSheet.Name().Data()));
QString aSheetName = (aSheetSourceName.isEmpty()) ? "Sheet_" + aSheetIndexStr : aSheetSourceName;
auto aSheetNode = aFactory.CreateGraph (aSheet);
if (aSheetNode) {
aSheetNode.SetVisibilityMode (ModelPrs_VM_Hidden);
myRoot.AddChildNode (aSheetNode);
myDrawingSheetNodes.push_back (aSheetNode);
aSheetNamesList.push_back (aSheetName);
if (aSheetName == QLatin1String ("Model")) {
aModelSheetIndex = aSheetIndex - 1;
}
}
}
if (aModelSheetIndex < myDrawingSheetNodes.size()) {
myDrawingSheetNodes[aModelSheetIndex].SetVisibilityMode (ModelPrs_VM_Visible);
}
emit sceneNodeCreated (aSheetNamesList);
}
void DrawingViewerApplication::onInitialized()
{
myMainWindow->setProperty ("fileDialogNameFilters", "DWG files (*.dwg)");
myViewPort->setCameraPositionType (ModelPrs_CMT_Top);
}
void DrawingViewerApplication::onCurrentSheetIndexChanged (int thePreviousIndex, int theCurrentIndex)
{
int aSize = static_cast<int> (myDrawingSheetNodes.size());
if (thePreviousIndex == 0 || theCurrentIndex == 0) {
for (int i = 0; i < aSize; i++) {
auto aMode = theCurrentIndex == 0 || theCurrentIndex - 1 == i ? ModelPrs_VM_Visible : ModelPrs_VM_Hidden;
myDrawingSheetNodes[i].SetVisibilityMode (aMode);
}
} else {
if (thePreviousIndex > 0 && thePreviousIndex <= aSize) {
myDrawingSheetNodes[thePreviousIndex - 1].SetVisibilityMode (ModelPrs_VM_Hidden);
}
if (theCurrentIndex > 0 && theCurrentIndex <= aSize) {
myDrawingSheetNodes[theCurrentIndex - 1].SetVisibilityMode (ModelPrs_VM_Visible);
}
}
myScene.Update();
myScene.Wait();
myViewPort->animatedFitAll();
}
void DrawingViewerApplication::onSceneNodeCreated (const QStringList& theSheetNamesList)
{
myMainWindow->setProperty ("sheetNamesModel", theSheetNamesList);
connect (myMainWindow, SIGNAL (currentSheetIndexChanged (int,int)),
this, SLOT (onCurrentSheetIndexChanged (int,int)));
}
void DrawingViewerApplication::Clear()
{
disconnect (myMainWindow, SIGNAL (currentSheetIndexChanged (int,int)),
this, SLOT (onCurrentSheetIndexChanged (int,int)));
myMainWindow->setProperty ("sheetNamesModel", {});
myDrawingSheetNodes.clear();
BaseViewerApplication::Clear();
}
bool ReadDrawing() const
Specifies whether a drawing should be read from the file. This is an overloaded member function,...
Definition: Base_ReaderParameters.cxx:194
Defines parameters of the DWG reader.
Definition: DWG_ReaderParameters.hxx:27
Iterator over sheets of a drawing.
Definition: ModelData_Drawing.hxx:51
Creates a scene nodes and its children from input data model objects.
Definition: ModelPrs_SceneNodeFactory.hxx:53
ModelPrs_SceneNode CreateGraph(const ModelData_Model &theModel, ModelData_RepresentationMask theRepresentationMask)
Creates scene graph using ModelData_Model.
Definition: ModelPrs_SceneNodeFactory.cxx:735
void SetVisibilityMode(ModelPrs_VisibilityMode theMode)
Sets theMode as visibility mode.
Definition: ModelPrs_SceneNode.cxx:842
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
@ ModelPrs_VM_Visible
Fully visible.
Definition: ModelPrs_VisibilityMode.hxx:30
@ ModelPrs_VM_Hidden
Fully unvisible.
Definition: ModelPrs_VisibilityMode.hxx:32

DrawingViewerWindow.qml

import QtQuick 2.12
import QtQuick.Controls 2.3
import CadEx 1.2
BaseWindow {
id: root
property alias sheetNamesModel: sheetNamesRepeater.model
signal currentSheetIndexChanged (int thePreviousIndex, int theCurrentIndex)
onSheetNamesModelChanged: {
buttonGroup.currentIndex = -1;
var aCount = sheetNamesRepeater.count;
if (aCount > 1) {
var aFind = false;
for (var i = 0; i < aCount; i++) {
if (sheetNamesRepeater.itemAt (i) && sheetNamesRepeater.itemAt (i).text === "Model") {
sheetNamesRepeater.itemAt (i).checked = true;
aFind = true;
break;
}
}
if (!aFind) {
sheetNamesRepeater.itemAt (0).checked = true;
}
} else if (sheetNamesRepeater.itemAt (0)) {
sheetNamesRepeater.itemAt (0).checked = true;
}
}
onMenuCompleted: {
menuBarItem.addMenu (displayedSheet);
}
Menu {
id: displayedSheet
title: qsTr ("&Displayed sheet")
ButtonGroup {
id: buttonGroup
property int currentIndex: -1
onCheckedButtonChanged: {
if (checkedButton) {
var aNewIndex = checkedButton.itemIndex;
currentSheetIndexChanged (currentIndex, aNewIndex);
currentIndex = aNewIndex;
} else {
currentIndex = -1;
}
}
}
Repeater {
id: sheetNamesRepeater
MenuItem {
text: modelData
checkable: true
ButtonGroup.group: buttonGroup
property int itemIndex: index
}
}
}
ModelPrsQtQuick_ViewPort {
id: viewPort
objectName : "viewPort"
anchors.fill : parent
antialiasing : true
highlightEnabled: false
viewCubeEnabled : false
mouseMode : ModelPrsQtQuick_ViewPort.Fixed2d
backgroundStyle: ModelPrsQtQuick_BackgroundStyle {
topColor : "#303030"
bottomColor: "#303030"
}
}
}

main.cxx

// ****************************************************************************
// $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.
//
// ****************************************************************************
#include "DrawingViewerApplication.hxx"
#include <cadex/LicenseManager_Activate.h>
#include <QtCore/QUrl>
#include <QtWidgets/QApplication>
#include "../../../cadex_license.cxx"
using namespace cadex;
int main (int argc, char *argv[])
{
auto aKey = LicenseKey::Value();
//activate the license (aKey must be defined in cadex_license.cxx)
if (!CADExLicense_Activate (aKey)) {
qCritical ("Failed to activate CAD Exchanger license.");
return 1;
}
// Must set dpi scaling before creating an application.
QCoreApplication::setAttribute (Qt::AA_EnableHighDpiScaling);
// Create QApplication because Qt.labs.platform module requires Qt Widgets library.
// For more information see https://doc.qt.io/qt-5.10/qml-qt-labs-platform-filedialog.html
QApplication app (argc, argv);
DrawingViewerApplication anApp;
if (anApp.Initialize (QUrl ("qrc:/qml/DrawingViewerWindow.qml"), "viewPort")) {
return app.exec();
}
return 0;
}