Import SolidWorks model example (demo_PY_import_solidworks.cpp)
Learn how you can use few C++ lines of code to load a mechanism that you exported from SolidWorks.
This tutorial shows how to:
- load a SolidWorks mechanism into Chrono::Engine
- retrieve pointers to SolidWorks exported parts, given their name
- show a realtime simulation with Irrlicht
// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Authors: Alessandro Tasora
// =============================================================================
//
// Demo code about
// - loading a SilidWorks .py file saved with the Chrono::Engine add-in,
// - showing the system in Irrlicht.
//
// =============================================================================
#include "chrono_irrlicht/ChIrrApp.h"
#include "chrono_python/ChPython.h"
#include <irrlicht.h>
// Use the namespace of Chrono
using namespace chrono;
using namespace chrono::collision;
using namespace chrono::irrlicht;
// Use the main namespaces of Irrlicht
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
int main(int argc, char* argv[]) {
// Create a Chrono::Engine physical system
ChSystemNSC mphysicalSystem;
// Set the collision margins. This is expecially important for
// very large or very small objects! Do this before creating shapes.
//
// LOAD THE SYSTEM
//
// The Python engine. This is necessary in order to parse the files that
// have been saved using the SolidWorks add-in for Chrono::Engine.
ChPythonEngine my_python;
try {
// This is the instruction that loads the .py (as saved from SolidWorks) and
// fills the system.
// In this example, we load a mechanical system that represents
// a (quite simplified & approximated) clock escapement, that has been
// modeled in SolidWorks and saved using the Chrono Add-in for SolidWorks.
mphysicalSystem); // note, don't type the .py suffix in filename..
} catch (ChException myerror) {
GetLog() << myerror.what();
}
// From this point, your ChSystem has been populated with objects and
// assets load from the .py files. So you can proceed and fetch
// single items, modify them, or add constraints between them, etc.
// For example you can add other bodies, etc.
// Log out all the names of the items inserted in the system:
GetLog() << "SYSTEM ITEMS: \n";
mphysicalSystem.ShowHierarchy(GetLog());
}
}
}
}
// Fetch some bodies, given their names,
// and apply forces/constraints/etc
std::shared_ptr<ChPhysicsItem> myitemE = mphysicalSystem.Search("escape_wheel^escapement-1");
std::shared_ptr<ChPhysicsItem> myitemA = mphysicalSystem.Search("truss^escapement-1");
std::shared_ptr<ChPhysicsItem> myitemB = mphysicalSystem.Search("balance^escapement-1");
std::shared_ptr<ChPhysicsItem> myitemC = mphysicalSystem.Search("anchor^escapement-1");
auto mescape_wheel = std::dynamic_pointer_cast<ChBody>(myitemE);
auto mtruss = std::dynamic_pointer_cast<ChBody>(myitemA);
auto mbalance = std::dynamic_pointer_cast<ChBody>(myitemB);
auto manchor = std::dynamic_pointer_cast<ChBody>(myitemC);
if (mescape_wheel && mtruss && mbalance && manchor ) {
// Set a constant torque to escape wheel, in a
// very simple way:
mescape_wheel->Set_Scr_torque(ChVector<>(0, -0.03, 0));
// Add a torsional spring
std::shared_ptr<ChLinkLockFree> mspring(new ChLinkLockFree);
mspring->Initialize(mtruss, mbalance, CSYSNORM); // origin does not matter, it's only torque
mspring->GetForce_Ry().SetK(0.24);
mspring->GetForce_Ry().SetActive(1);
mphysicalSystem.Add(mspring);
// Set an initial angular velocity to the balance:
mbalance->SetWvel_par(ChVector<>(0, 5, 0));
// Set no friction in all parts
mbalance->GetMaterialSurfaceNSC()->SetFriction(0);
mescape_wheel->GetMaterialSurfaceNSC()->SetFriction(0);
manchor->GetMaterialSurfaceNSC()->SetFriction(0);
} else
GetLog() << "Error: cannot one or more objects from their names in the C::E system! \n\n";
//
// THE VISUALIZATION
//
// Now, suppose one is interested in showing an animation of
// the simulated system. There are different options, for instance
// one could use the unit_POSTPROCESS approach for rendering in
// POVray, or you can open an Irrlicht 3D realtime view and show
// it, as in the following example code:
// Create the Irrlicht visualization (open the Irrlicht device,
// bind a simple user interface, etc. etc.)
ChIrrApp application(&mphysicalSystem, L"Import a SolidWorks system", core::dimension2d<u32>(800, 600), false);
// Easy shortcuts to add camera, lights, logo and sky in Irrlicht scene:
application.AddTypicalLogo();
application.AddTypicalSky();
application.AddTypicalCamera(vector3df(0.0f, 0.25f, 0.25f), vector3df(0.0f, 0.0f, -0.1f));
application.AddLightWithShadow(vector3df(-0.5f, 0.5f, 0.0f), vector3df(0, 0, 0), 1, 0.2, 1.2, 30, 512,
video::SColorf(1.0f, 0.9f, 0.9f));
application.AddLightWithShadow(vector3df(0.5f, 0.5f, 0.5f), vector3df(0, 0, 0), 1, 0.2, 1.2, 30, 512,
video::SColorf(0.6f, 0.8f, 1.0f));
// ==IMPORTANT!== Use this function for adding a ChIrrNodeAsset to all items
// in the system. These ChIrrNodeAsset assets are 'proxies' to the Irrlicht meshes.
// If you need a finer control on which item really needs a visualization proxy in
// Irrlicht, just use application.AssetBind(myitem); on a per-item basis.
application.AssetBindAll();
// ==IMPORTANT!== Use this function for 'converting' into Irrlicht meshes the assets
// that you added to the bodies into 3D shapes, they can be visualized by Irrlicht!
application.AssetUpdateAll();
// This is to enable shadow maps (shadow casting with soft shadows) in Irrlicht
// for all objects (or use application.AddShadow(..) for enable shadow on a per-item basis)
application.AddShadowAll();
//
// THE SIMULATION LOOP
//
// set a low stabilization value because objects are small!
application.GetSystem()->SetMaxPenetrationRecoverySpeed(0.002);
application.SetStepManage(true);
application.SetTimestep(0.002);
application.SetTryRealtime(true);
while (application.GetDevice()->run()) {
application.BeginScene();
application.DrawAll();
application.DoStep();
application.EndScene();
}
return 0;
}
std::string GetChronoDataFile(const std::string &filename)
Obtain the complete path to the specified filename, given relative to the Chrono data directory (thre...
Definition: ChGlobal.cpp:95
const std::vector< std::shared_ptr< ChLinkBase > > & Get_linklist() const
Get the list of links.
Definition: ChAssembly.h:101
static void SetDefaultSuggestedMargin(double mmargin)
Using this function BEFORE you start creating collision shapes, it will make all following collision ...
Definition: ChCCollisionModel.cpp:40
const std::vector< std::shared_ptr< fea::ChMesh > > & Get_meshlist() const
Get the list of meshes.
Definition: ChAssembly.h:103
void ShowHierarchy(ChStreamOutAscii &m_file, int level=0)
Writes the hierarchy of contained bodies, markers, etc.
Definition: ChAssembly.cpp:1115
ChLog & GetLog()
Global function to get the current ChLog object.
Definition: ChLog.cpp:39
void SetTimestep(double val)
Set/Get the time step for time integration.
Definition: ChIrrAppInterface.cpp:532
void SetStepManage(bool val)
If set to true, you can use DoStep() in the simulation loop to advance the simulation by one timestep...
Definition: ChIrrAppInterface.h:84
irr::scene::ILightSceneNode * AddLightWithShadow(irr::core::vector3df pos, irr::core::vector3df aim, double radius, double mnear, double mfar, double angle, irr::u32 resolution=512, irr::video::SColorf color=irr::video::SColorf(1.f, 1.f, 1.f, 1.f), bool directional=false, bool clipborder=true)
Add a point light that cast shadow (using soft shadows/shadow maps) Note that the quality of the shad...
Definition: ChIrrAppInterface.h:209
Class to add some GUI to Irrlicht+ChronoEngine applications.
Definition: ChIrrApp.h:29
virtual void EndScene()
Call this to end the scene draw at the end of each animation frame.
Definition: ChIrrAppInterface.cpp:578
void SetMaxPenetrationRecoverySpeed(double mval)
For the default stepper, you can limit the speed of exiting from penetration situations.
Definition: ChSystem.h:163
std::shared_ptr< ChPhysicsItem > Search(const char *name)
Search an item (body, link or other ChPhysics items) by name.
Definition: ChAssembly.cpp:304
const std::vector< std::shared_ptr< ChPhysicsItem > > & Get_otherphysicslist() const
Get the list of physics items that are not in the body or link lists.
Definition: ChAssembly.h:105
const std::vector< std::shared_ptr< ChBody > > & Get_bodylist() const
Get the list of bodies.
Definition: ChAssembly.h:99
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
virtual void DoStep()
Call this important function inside a cycle like while(application.GetDevice()->run()) {....
Definition: ChIrrAppInterface.cpp:590
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChAssembly.cpp:190
static void SetDefaultSuggestedEnvelope(double menv)
Using this function BEFORE you start creating collision shapes, it will make all following collision ...
Definition: ChCCollisionModel.cpp:36
virtual void BeginScene(bool backBuffer=true, bool zBuffer=true, irr::video::SColor color=irr::video::SColor(255, 0, 0, 0))
Call this to clean the canvas at the beginning of each animation frame.
Definition: ChIrrAppInterface.cpp:559
void ImportSolidWorksSystem(const char *solidworks_py_file, ChSystemNSC &msystem)
Load a .py file as it is saved by the SolidWorks add-in exporter.
Definition: ChPython.cpp:228
virtual void DrawAll()
Call this important function inside a loop like while(application.GetDevice()->run()) {....
Definition: ChIrrAppInterface.cpp:699
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29
void SetTryRealtime(bool val)
If set to true, the function DoStep() will try to use a timestep that is the same as that used to ref...
Definition: ChIrrAppInterface.h:92