Hopsan
|
When you create a new component, you are actually creating a new component library. The library may contain one single component or multiple components. If you are creating multiple components, it is recommended to put them in the same library.
A Hopsan component library consists of at least three types of files.
There needs to be one .xml file for every component in the library but .svg files can be shared if needed. All components should be compiled into one single .dll file. Subdirectories containing the component files can be used to create sub categories in the library.
If you have access to the component library source code, some additional important files should also be mentioned.
The header-only .hpp files contains the source code for each components, see Writing The Actual Component Code.
The .xml files contain the appearance description, and the .svg files are the icons, see Create or Modify Component Appearance.
This xml file, introduced in Hopsan version 0.7, contains the necessary information for Hopsan GUI to recognice which file is the component library .dll. This is required if a library in itself dependes on other third party libraries. The file also contains information necessary for automatic (re)compilation by Hopsan. This file is also used as the "project file" when editing component libraries from within Hopsan.
exampleComponentLib.xml
<?xml version="1.0" encoding="UTF-8"?> <hopsancomponentlibrary version="0.2"> <!-- The "id" element gives the library a unique identity, a UUID is recommended but any "unique" string is accepted --> <id>7f871523-08e4-4d68-a7e9-25860d861888</id> <!-- The 'name' element gives the library a human readable name that will be used in the library widget --> <name>HopsanExampleComponentLibrary</name> <!-- The "lib" element defines the name of the (.dll / .so / .dynlib) file, (without file extension) --> <!-- Use the "debug_ext" attribute to define the optional debug extension, used in debug mode --> <lib debug_ext="_d">examplecomponentlib</lib> <!-- Use one or more "source" elements to define the files that should be compiled and linked into the library --> <source>exampleComponentLib.cpp</source> <!-- The "buildflags" element can be used to specify optional compiler and linker flags necessary when compiling the library --> <buildflags> <!-- One or multiple "cflags" elements defines the compiler flags --> <!-- One or multiple "lflags" elements defines the linker flags --> <!-- use the "os" attribute to restrict the flag to a certain platform --> <!-- Example: <cflags>-I./someLib/include</cflags> <lflags>-L./someLib/bin -lsomeLib</lflags> <lflags os="win64">-lWs2_32</lflags> --> </buildflags> <!-- Use one "component" element for every component source code .hpp (header only implementation) file --> <!-- This list of components will be used by the Hopsan library export code --> <component>HydraulicComponents/MyExampleConstantOrifice.hpp</component> <component>HydraulicComponents/MyExampleOrifice.hpp</component> <component>HydraulicComponents/MyExampleVolume.hpp</component> <component>HydraulicComponents/MyExampleVolume2.hpp</component> <component>SignalComponents/MyExampleSignalSum.hpp</component> <!-- Use one "componentxml" element for every component description XML file --> <componentxml>SignalComponents/MyExampleSignalSum.xml</componentxml> <componentxml>HydraulicComponents/MyExampleConstantOrifice.xml</componentxml> <componentxml>HydraulicComponents/MyExampleOrifice.xml</componentxml> <componentxml>HydraulicComponents/MyExampleVolume.xml</componentxml> <componentxml>HydraulicComponents/MyExampleVolume2.xml</componentxml> </hopsancomponentlibrary>
This is the project file used by Qt Creator. It is used by qmake to generate a Makefile (compilation script). If you are using a different coding environment, you need to create a similar file for that environment. It is also possible to write a custom Makefile manually. It basically comes down to specifying the include path to the HopsanCore include files and the link path to the pre-compiled HopsanCore shared object (.dll, .so, .dynlib) file or link library (.lib, .a).
A few modifications are required in this file. First of all the INCLUDEPATH must be changed so that it points to the "include" folder of the HopsanCore installation. Similarly, the LIBS path must point to the "bin" folder in the installation directory. You should also change the TARGET to the name you wish your compiled library (.dll) to have. Finally, the relative path to all components source files in the library must be specified under HEADERS. (Assuming you have written each component in a header-only file).
exampleComponentLib.pro
# QT -= core gui, means that we should not link the default qt libs into the component # Template = lib, means that we want to build a library (.dll or .so) QT -= core gui TEMPLATE = lib # TARGET is the name of the compiled lib, (.dll or .so will be added automatically) # Change this to the name of YOUR lib TARGET = examplecomponentlib # Destination for the compiled dll. $${PWD}/ means the same directory as this .pro file, even if you use shadow build DESTDIR = $${PWD}/ # The location to search for the Hopsan include files. By specifying the path here, you dont need to # do this in each of your component .hpp files. You can also add additional paths for any custom Utility functions, # just add additional INCLUDEPATH *= ... lines. # *= Means append unique INCLUDEPATH *= $${PWD}/../../HopsanCore/include/ #INCLUDEPATH *= "C:/SomeDirectoryPath/Hopsan/HopsanCore/include/" # The location of the HopsanCore .dll or .so file, needed to link against when compiling your library LIBS *= -L$${PWD}/../../bin #LIBS *= -L"C:/SomeDirectoryPath/Hopsan/bin/" # Special options for deug and release mode. This will link the correct HopsanCore .dll or .so # In debug mode HopsanCore has the debug extension _d include(hopsanDebugReleaseCompile.pri) # Reduce compile output clutter, but show warnings CONFIG += silent warn_on # The compiler should be pedantic to catch all errors (optional) QMAKE_CXXFLAGS += -pedantic # Enable C++14 CONFIG += c++14 # Enable the use of M_PI and such DEFINES *= _USE_MATH_DEFINES # ------------------------------------------------- # Project files # ------------------------------------------------- SOURCES += \ exampleComponentLib.cpp HEADERS += \ HydraulicComponents/MyExampleVolume.hpp \ HydraulicComponents/MyExampleOrifice.hpp \ SignalComponents/MyExampleSignalSum.hpp \ HydraulicComponents/MyExampleVolume2.hpp \ HydraulicComponents/MyExampleConstantOrifice.hpp OTHER_FILES += \ hopsanDebugReleaseCompile.pri
This file contains code that is used to register you library components in the HopsanCore. A few modifications are required.
#include
directive will actually in-place copy the included files into the cpp file prior to compilation)get_hopsan_info
function. Leave the two last lines as they are.exampleComponentLib.cpp