You are on page 1of 20

Windchill Modeler Automatic Code

Synchronization (ACS) Tutorial

Version 9.4
Copyright © 2021 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
User and training guides and related documentation from PTC Inc. and its subsidiary companies (collectively "PTC") are
subject to the copyright laws of the United States and other countries and are provided under a license agreement that
restricts copying, disclosure, and use of such documentation. PTC hereby grants to the licensed software user the right to
make copies in printed form of this documentation if provided on software media, but only for internal/personal use and in
accordance with the license agreement under which the applicable software is licensed. Any copy made shall include the
PTC copyright notice and any other proprietary notice provided by PTC. Training materials may not be copied without the
express written consent of PTC. This documentation may not be disclosed, transferred, modified, or reduced to any form,
including electronic media, or transmitted or made publicly available by any means without the prior written consent of
PTC and no authorization is granted to make copies for such purposes. Information described herein is furnished for
general information only, is subject to change without notice, and should not be construed as a warranty or commitment
by PTC. PTC assumes no responsibility or liability for any errors or inaccuracies that may appear in this document.
The software described in this document is provided under written license agreement, contains valuable trade secrets and
proprietary information, and is protected by the copyright laws of the United States and other countries. It may not be
copied or distributed in any form or medium, disclosed to third parties, or used in any manner not provided for in the
software licenses agreement except with written prior approval from PTC.
UNAUTHORIZED USE OF SOFTWARE OR ITS DOCUMENTATION CAN RESULT IN CIVIL DAMAGES AND CRIMINAL
PROSECUTION.
PTC regards software piracy as the crime it is, and we view offenders accordingly. We do not tolerate the piracy of PTC
software products, and we pursue (both civilly and criminally) those who do so using all legal means available, including
public and private surveillance resources. As part of these efforts, PTC uses data monitoring and scouring technologies to
obtain and transmit data on users of illegal copies of our software. This data collection is not performed on users of legally
licensed software from PTC and its authorized distributors. If you are using an illegal copy of our software and do not
consent to the collection and transmission of such data (including to the United States), cease using the illegal version, and
contact PTC to obtain a legally licensed copy.

Important Copyright, Trademark, Patent, and Licensing Information: See the About Box, or copyright notice, of your PTC
software.

UNITED STATES GOVERNMENT RIGHTS


PTC software products and software documentation are “commercial items” as that term is defined at 48 C.F.R. 2.101.
Pursuant to Federal Acquisition Regulation (FAR) 12.212 (a)-(b) (Computer Software) (MAY 2014) for civilian agencies or
the Defense Federal Acquisition Regulation Supplement (DFARS) at 227.7202-1(a) (Policy) and 227.7202-3 (a) (Rights in
commercial computer software or commercial computer software documentation) (FEB 2014) for the Department of
Defense, PTC software products and software documentation are provided to the U.S. Government under the PTC
commercial license agreement. Use, duplication or disclosure by the U.S. Government is subject solely to the terms and
conditions set forth in the applicable PTC software license agreement.

PTC Inc., 121 Seaport Blvd, Boston, MA 02210 USA

2
Contents
Prerequisites ........................................................................................................................... 4
Automatically Synchronizing Model and Code ........................................................................ 5
Assumptions and requirements .......................................................................................... 5
Developing the initial application........................................................................................ 6
Handling Change ............................................................................................................... 17

3
Prerequisites

This tutorial provides an introduction to the principles and use of Automatic Code
Synchronization (ACS). It uses the C++ language, although anyone with some understanding
of Object Oriented development would be able to benefit from the use of this tutorial. A
knowledge of C++ is not essential.

The tutorial is provided as a PDF file. If you only have a single monitor, you may find it easier
to print the tutorial and follow it on paper. However, you may also want to keep the PDF
document open to allow you to copy and paste code fragments from the PDF file into the
Windchill Modeler model. If you need to install Adobe Acrobat, to view or print the PDF
files, you can do so by downloading it from the Adobe web site (http://www.adobe.com).

You will need an appropriate language development environment to use the ACS tutorial.
We have used Microsoft® Visual Studio Community Edition. This is freely available from
http://www.microsoft.com/express/product/default.aspx.

You will not need any other files or supplied Windchill Modeler models for this tutorial.

Should have you have any problems with, or suggestions for, this tutorial please contact us
via your PTC support account.

4
Automatically Synchronizing Model and Code
Many systems and software engineers recognize the benefits of Windchill Modeler
modeling for capturing system requirements and the subsequent system design. However,
the design needs to be transformed into code at some point and this code needs to be
tested to ensure that it conforms to the design and the requirements. The process of
transforming the design into code is made much faster and more consistent if a code
generator is used. Subsequent code and model changes, as a consequence of testing
activity, imply the need to synchronize code and model in order that code changes are
reflected in the model and vice-versa. Windchill Modeler provides automatic code
synchronization (ACS) with automatic and immediate updating of both code and model as
changes are made to either. ACS provides support for Ada, C, C++, C#, IDL, Java and VB code.

With ACS you don’t have to worry about the model becoming out-of-step with the code;
drift is prevented because synchronization is instantaneous and automatic. You don’t have
to configure your code separately since it is contained within the model and can be
regenerated at any time.

ACS also acts as a mentor, instilling a mastery of the mapping between design and code. It
helps you to understand that the model and code are just two views of the same artifact.
When you change the design in the model, the corresponding change will be reflected in the
code.

The goal of this tutorial is to show how you can quickly design and build an application using
Windchill Modeler with C++ as a target language.

You may find it helpful to have the PDF version of this document open on your computer.
This will allow you to copy code bodies from the document (using Adobe Reader's Select
Tool, for example) and paste them into your code editor.

Assumptions and requirements

This section assumes some familiarity with both UML class modeling and Windchill Modeler.
You can obtain the required level of familiarity from Part 1 of the Windchill Modeler tutorial
and the Part 2 section on the use of the class diagram.

This section covers the use of ACS in a C++ environment. Some understanding of C++ is
therefore assumed, although non-C++ people will still gain much benefit from an
understanding of the principle of ACS.

This section of the tutorial requires the use of a C++ development environment that includes
a set of standard library files, such as that supplied with Microsoft Visual C++. We have used
Microsoft® Visual Studio Community Edition which is freely downloadable form
http://www.microsoft.com/express/product/default.aspx. If you have an alternative
development environment, you may need to modify some of the activities.

5
Developing the initial application

The application is a simple screen saver: a ball


character that moves in the Command Prompt
window, bouncing when it hits the screen
boundaries. (It is based on the BallCpp model
supplied in the Examples repository that is created
by default with a Windchill Modeler installation).

The platform used is Win32 Console.

In order to make the application simple, the character can move only in a 45° direction.

The character's attributes are X and Y (position coordinates), and Vx and Vy (velocity). The
values of Vx and Vy, and the resulting direction of movement, are as follows:

Vx Vy Direction
+1 +1
+1 -1
-1 +1
-1 -1

To begin you will need to create a new Windchill Modeler model that has the capabilities to
allow you to use ACS with C++. These capabilities are provided through the addition to the
model of relevant profiles. A profile is a package of stereotypes, tags and possibly some
associated items which extend Windchill Modeler's modeling capabilities in some way.

Profiles can be added to a Windchill Modeler model either when the model is created or at
some later time. You will add the required profiles as you create the model.

1. Open Windchill Modeler .


2. A Start Page will appear, select Create a New Model.

The opens the New Model dialog (see the following image), from which you can select the
profile(s) you need in the model.

You will need to include the ACS and the C++ profiles into this model. The role played by the
ACS profile is to notify the ACS system each time either something has changed by the user
in the Windchill Modeler model or if a code body changes in the source files. The C++ profile
will allow you to incorporate C++ language specific concepts into your model.

6
3. Select the C++ checkbox (as above) – this will automatically select the ACS and
Thingworx checkboxes.
4. In the Model Name input box enter the name MyBallCpp as in the above and
choose a database to store the model. Click the OK button.

The model, with these profiles, will now be created in the Models repository, with progress
being shown in the Creating Model… window.

In the Packages (browser) pane you will see the three locked
profile packages (as opposite). The profiles have a small lock
image on the icon to indicate they are protected (read-only).
A model owner could unprotect them, but this is not
advisable. Profiles are standard libraries; their contents
should not be altered; doing so can have unpredictable and
severe consequences for your model.

We will need to create another top-level package to hold the model items we will generate
code for.

5. Right-click the model name in the browser pane, select New->Package and name
the new package Balls.

For the ACS profile to do its notification work, the ACS system must be loaded and started.
This is done using the ACS/TDK Control ribbon bar. The ACS/TDK Control ribbon bar is shown
below.

7
6. Click the ‘Load or Unload ACS’ button on this ribbon to load ACS.

This enables the New Scheme… drop-down list on the ribbon to allow you to select a
previously created scheme or to create a new generation settings scheme. The status
indicator below the scheme drop-down list should now show “Connected”.

You will also see a new output pane is enabled; this is called the ACS/TDK Log. This pane is
used to display messages from the ACS system.

7. Select the Balls package in the Packages


browser pane.
8. Ensure that New Scheme… is showing in the
ACS/TDK Control ribbon drop-down entry and
click the Edit ACS Scheme ribbon button.

This opens the Launch dialog which allows you to define the information required by ACS.
You will see that two of the fields are pre-filled: Model and Model Root Object. The latter
defines the scope of the code synchronization within the model. The root object will be the
currently selected item in the browser pane. This is normally a package (for us, the Balls
package) but we could have selected the entire model, indicating that you want to generate
everything within it. We will leave the root object as the Balls package.

This dialog also allows you to specify a Settings File. This file is used to save the selection
options, to save time on future use of ACS with this model.

8
9. Create a suitable Settings File (select the New Settings File icon and Open it in
a directory of choice called it MyBallCpp.sni).

You will need to select what Generator Dll to use for the code generation process to
translate the model information to code file text. The Help system tells you the different
options here; we are going to use a C++ Win32 DLL, that will generate C++ code and a
project file suitable for Visual Studio.

10. Use the cpp win 32 production Generator Dll, click the browse icon , to select
and open the DLL in the CppGen\Cpp Win32 Production\ directory.

This will populate the Generator Dll, Generator Timestamp and Generator Dll Folder fields.

11. Set the Target Directory to C:\Temp\Code\MyBallCpp (for instance) to define this
as the directory in which generated code will be placed.

The Launch Options box allows you to specify ACS/TDK parameters. We will not need to do
this, but parameter details can be found in the ACS/TDK section of the Help system.

12. Now select the Advanced tab.

The options here are self-explanatory and can be left to their defaults (defined by the DLL
selection).

13. Finally, click the Save and Launch button.

The ACS/TDK Log pane is populated with an event log similar to that below.

ACS performs the following operations:

• Opens the current model (if not already open). This may take some seconds
• Performs the “First Code Generation”. The first code generation is provided just for
letting ACS determine the target file names for generated code, and in order to
check timestamps for reverse engineering. The first code generation does not
generate any code
• Activates the model/code change scanner, which checks continuously for changes to
the model and code

ACS is now up and running and you can largely forget about it, although it’s there in the
background synchronizing model and code. You should look at the ACS/TDK Log pane

9
occasionally, especially immediately after updating the model or code files, where you will
see evidence of ACS doing its job.

Our first example will have a single function main.

14. Right-click the Balls package and select New->UML->Behaviour->Operation to


create an operation named main.

Inspect the ACS/TDK Log pane. Within a few seconds, it should detect that the model has
been updated and initiate a generation operation.

In the target directory that was selected when launching ACS (e.g.
C:\Temp\Code\MyBallCpp ), you will now see the .h and .cpp files for the Balls package,
plus the StdAfx files and a .vcxproj files for the DLL scheme you selected.

You now need to create a new empty Win32 Application project, called MyBallCpp, using
your C++ development environment, to include these files.

15. Double-click the .vcxproj file to open Microsoft Visual Studio to create a new Win32
Application project. Among the .vcxproj available, select the one suitable for your
Visual studio edition
16. Rename the project MyBallCpp.
17. On the Tools menu, click Options. In the options dialog, select the Documents entry
in the Environment tree and ensure that the 'Detect when file is changed outside
the environment' and ' Reload modified files unless there are unsaved changes’
checkboxes are selected. Click the OK button.

Working with ACS is more effective when this capability is enabled.

By default, Modeler ACS adds four additional libraries to the project for advance code
generation particularly for PTC’s Internet of Things capability Thingworx. For a simple
compilation of code these are
not needed and need to be
removed for the tutorial.

18. On the Project menu


select Properties. In the
new dialog expand
Linker. Select General
and click in the
Additional Library
Directories section and
select edit. Remove all
the additional libraries
except for the Microsoft
SDK, then click OK.

10
19. Select the Input option, click in the Additional Dependencies and select edit. Delete
the following dependencies:

AxTLSLib.lib
TheClient.lib
CppConcurrency.lib
UtilityLibs.lib

20. Within Microsoft Visual Studio, open the Balls.cpp file, located under the project
folder Source Files, and identify the body of the main function.

There are two comments in it:

// ## OperationBody [………………………………]

and:

// ## OperationBody End

These comments are added by ACS and are used to associate operation bodies in the code
files with their equivalent in the model.

Before writing the body between these comments, observe that there is a hexadecimal tag:

// ## OperationBody [xxx…]

Windchill Modeler is a repository-based tool. In a repository every entity within every model
has a unique ID number. The tagged identifier we see is the ID which corresponds in our
repository to the main operation.

When you created the Windchill Modeler model, you included the C++ Profile. If you expand
the C++ Profile Package on the Package browser, you see that below a package called Scripts
it contains two categories of elements: Stereotypes and Tag Definitions.

A Profile (in this case, C++) is added because UML doesn’t know anything about
implementation languages. UML has been conceived as a visual modeling notation, not a
visual programming notation. There is no link between UML and any implementation
language. To generate code from a UML model, we have to provide UML with some
additional, language specific information. This additional information comes from the
Stereotypes and Tag Definitions included in a language profile, such as the C++ Profile.

11
Stereotypes

Type
Definitions

21. In the Balls package, create a new Class Diagram (right-click the package and
select New->Diagram->UML->Class Diagram).
22. Give the Class Diagram the same name as the package, that is, Balls.

23. On the class diagram, create a class (using the Class ribbon icon ) and name it
TBall.

We have chosen to prefix class names with “T” for denoting that they are types.

24. Add a new operation to the TBall class (use the Operation ribbon icon ), name
it Draw.

Note the effect in the generation directory of your file system.

25. Go to Microsoft Visual Studio and notice the TBall.cpp and TBall.h are part of your
project.
12
26. Open TBall.h and observe its content.

There is a single class defined inside, which starts with the keyword class.

class TBall

Note that there is a sort of function prototype declared inside the structure scope.

void Draw();

The effect of putting prototypes like this inside the class is that the functions automatically
receive the base class pointer parameter that is implicitly declared, although we cannot see
it – it’s just C++ compiler stuff.

As TBall represents the ball, it must have X and Y values for its position coordinates and the
Vx and Vy components of its velocity. These data items will be added as class attributes.

27. In the Windchill Modeler window, right-click the TBall class in the Package browser
pane, select New->Attribute. Name the attribute X.
28. In the attribute property sheets, click the Data Type tab, and then select the ‘Use
existing model data type’ check box.
29. In the resulting Select Datatype
dialog box, select All Types in
the left-hand pane, and then
double-click the int datatype in
the right hand pane, to set the
Data Type as opposite.
30. In the Packages browser pane,
clone the X attribute by
dragging it to the TBall class
while 2keeping the Ctrl key
pressed.
31. Clone the X attribute a further
two times, and then change the names of the clones to Y, Vx and Vy.

The browser and the diagram should now


look something like that opposite.

You will also see these changes reflected in


the TBall.h code file in the Microsoft Visual
Studio window.

32. In Windchill Modeler, add another


operation to TBall, name it Move.
33. Select the Draw operation you previously added and open its Properties pages.
Select the Sig’ture tab and change its signature as follows:

Draw (in What : bool)

13
As usual, click anywhere outside the modified field in order to save the changes.
Note that Windchill Modeler creates the parameters and recognizes bool as a data type
from the C++ Profile.

34. Add another operation named Init to TBall, and change its signature as follows:

Init (in PX : int, in PY : int)

35. Go back to Microsoft Visual Studio. Open the TBall.cpp file, locate the Draw
operation, and enter the following code between the two comments // ##
OperationBody [...] and // # OperationBody End:

HANDLE H = GetStdHandle (STD_OUTPUT_HANDLE);


COORD Q;
Q.X = X;
Q.Y = Y;
SetConsoleCursorPosition (H, Q);
if (What) printf ("O");
else printf (" ");

36. Try to build TBall.cpp.

There are some errors as we are using some functions to access the Command Prompt
window.

37. Before we fix the errors, open the properties for the Draw operation of the TBall
class in Windchill Modeler, and select the Body tab.

Here you will see the operation code you entered in Microsoft Visual Studio. ACS has
detected the code change and synchronized model with code.

Now let’s fix the compile errors.

38. In Windchill Modeler, right-click the TBall class (either in the browser or on the
diagram) and select Links->Stereotypes.
39. From the pop-up list of valid stereotypes, select C++ Class.
40. Select the TBall class in the Package browser or in the diagram pane and go to its
property sheets.
41. Select the newly created
C++ Class tab, select the
C++ Implementation
Include tag definition
and click the Properties
button.

This opens a full screen edit


window in which you can enter
text as the tag value.

14
42. In the editor window enter the following text:

#include <stdio.h>

43. Press Ctrl-S to save the changes, then press Ctrl-F4 to close the editor window.

With these changes made at model level, TBall.cpp should compile with no errors.

44. Go to Microsoft Visual C++ and check it compiles ok.


45. While in Visual C++, enter the following code for the Move operation body:

X += Vx;
Y += Vy;
if (X < X_MIN) {
X = X_MIN;
Vx = 1;
}
if (X > X_MAX) {
X = X_MAX;
Vx = -1;
}
if (Y < Y_MIN) {
Y = Y_MIN;
Vy = 1;
}
if (Y > Y_MAX) {
Y = Y_MAX;
Vy = -1;
}

If we compile there are some errors: X_MIN, X_MAX, Y_MIN and Y_MAX are undefined.
These are global values that define the edges of the window. We should go back to
Windchill Modeler again to add these to the model as attributes of the package.

46. In the Balls package, add an attribute named X_MIN (right-click the package and
select New->UML->Structure->Attribute).

We need to have a #define for X_MIN in the Balls.h file.

47. Select the X_MIN attribute and in the property sheets, set it up as Public and Read
Only (using the General and options tabs as below)

48. Select the Data Type tab and set a Default value of 1 but do not set a Type.

15
These last two activities will generate the required #define. You can open the Balls.h file to
confirm this if you wish.

49. Clone X_MIN three times, by dragging it to the Balls package while keeping the Ctrl
key pressed. Name the clones X_MAX (default value of 78), Y_MIN (default value of
1), and Y_MAX (default value of 22).
50. To make these Balls package attributes visible to TBall, drag the Balls package on
to the Balls class diagram.
51. Add a dependency
(use the Dependency

ribbon icon )
clicking first on the
TBall class symbol and
then on the Balls
package symbol. (Do
not name the
dependency).
52. In Microsoft Visual Studio, open the TBall.cpp file and note the addition of the
#include Balls.h entry.
53. In Microsoft Visual Studio, open the TBall.cpp file and enter the following text for
the Init operation body:

X = PX;
Y = PY;
Vx = 1;
Vy = 1;

Now if we build the solution for the project there should be no errors.

54. In Windchill Modeler, add an attribute named B to the Balls package.


55. Set the type of the B attribute to TBall, by dragging the TBall class on to the B
attribute in the Package browser.
56. Go back to Microsoft Visual Studio. In the Balls.cpp file, add to the main body the
following code between the OperationBody comments:

B.Init (1, 1);


while (1) {
B.Draw (true);
Sleep (100);
B.Draw (false);
B.Move();
}

57. Go to Microsoft Visual C++, compile, build and execute.

The application should run, as illustrated by the screen snapshot that follows.

16
Handling Change

You have now seen how ACS operates to allow changes in either model or code to be
automatically synchronized. If you wish you can skip this remaining subsection, which goes
on to look at how ACS can be used to handle an additional change, with the use of an
additional UML class modeling feature.

The specification of your project now requires more ball items in the Command Prompt
window. Rather than just one ball moving around, we now want a “train” seven to move
around.

This section shows how we need to develop the application, using ACS, to deal with this
changed requirement.

58. In Windchill Modeler, add a class TScreen to the class diagram (using the Class

ribbon icon ).
59. Add a composite
aggregation from TScreen to
TBall, as shown in the
following diagram (select the
Composite Aggregation

ribbon icon , click first


on TScreen, then on TBall,
and leave the default name,
Associates).

17
60. Open the composite aggregation
property sheets, and on the End
Role sheet, in role field, type Ball
(as opposite).
61. On the Start Role sheet, in the
second list box select 'user defined',
and then in the box that appears
type N_BALL (as Below).

62. Go to Microsoft Visual Studio


and you will be asked to reload the
project, click reload all and look at
the TScreen code.

Notice the data member Ball is


defined as a “vector” by means of
the template definition coming from
a special C++ library for containers
(Standard Template Library).

63. In Windchill Modeler, expand the TScreen class in the


browser (as opposite).

You will find a role element, called Ball typed by the TBall class.

64. Apply the C++ Role stereotype to the Ball role by right-clicking Ball and then
selecting Links->Stereotypes->C++ Role.

The Ball role has now acquired a new property sheet, C++ Role, as expected.

65. Click the C++ Role tab to open


this sheet. Double-click in the
Tag Value column for the C++
Indirection tag definition and
set its tag value to * (as
opposite).
66. Set the tagged value of the
C++ Array Spec tag definition
to [N_BALL], (as opposite).
67. Go to Microsoft Visual
Studio, look at TScreen.h file.

We now have: TBall * Ball[N_BALL]

18
which is what we want; we can now mix different objects in the array, because we process
their addresses, rather than the objects directly.

68. Go back to Windchill Modeler. In the TScreen class, create an attribute named
N_BALL.
69. Using the property sheets for N_BALL, set up the attribute as Public, Read Only
(Options tab); define no Type, but set a default value of 7 (Data Type tab).
70. Allow a short time for ACS to re-generate the TScreen.h file, then return back to
Microsoft Visual Studio to confirm the addition of the resulting #define.
71. In Windchill Modeler add to the TScreen class two operations named Init and Step.

ACS will now create the TScreen.cpp file.

72. In Microsoft Visual Studio, select Reload All and enter the following code for the
Init and Step bodies:

Init body:
int I;
for (I = 0; I < N_BALL; I ++) {
TBall* P = new TBall;
P->Init (I * 3 + 1, I * 2 + 1);
Ball [I] = P;
}

Step Body:
int I;
for (I = 0; I < N_BALL; I ++) {
Ball [I]->Draw (false);
Ball [I]->Move ();
Ball [I]->Draw (true);
}

73. In Windchill Modeler, add a new attribute to the Balls package and name it Screen.
74. Set the type of the Screen attribute to the TScreen class, by dragging the TScreen
class on to the Screen attribute in the Packages browser pane.
75. Delete the B attribute from Balls package (select Yes on the message box to
confirm the deletion).

The Balls package in the Packages browser pane should now


appear like that shown opposite.

76. Go to Microsoft Visual Studio. In the Balls.cpp file,


delete the existing main body and enter this new one:

Screen.Init ();
while (1) {
Screen.Step ();
Sleep (100);
}

77. Rebuild the solution in Microsoft Visual Studio and run.

19
There should now be seven “O”s bouncing around the Command Prompt window.

78. Finally, return to Windchill Modeler and view the Body property sheet for each
operation to confirm that model and code are completely synchronized.

It would clearly be possible to continue altering the specification of the problem and use
ACS as a means of co-developing both model and code. However, you have seen enough to
appreciate the features and benefits of ACS; how it lets you use UML to create a model
framework for your code and yet still use your IDE to debug and change key aspects of code
like operation bodies.

20

You might also like