Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Working with Specific Entitites > Working with Traces > Example of Working with the Trace Object
Example of Working with the Trace Object

This example demonstrates displaying and modifying properties of the trace object. This example uses a console menu that represents the following operations: getting and setting the first point [1], second point [2], third point [3], fourth point [4], normal [N], and thickness [T]; displaying the object properties, general properties, and specific properties [P]; and modifying the general entity properties [M].

The PrintTraceProperties() function requires a pointer to an existing trace object and displays its properties. The function gets the specific properties using the getPointAt(), normal(), and thickness() methods. The function uses the AboutPoint3d() function for converting the coordinates of the first, second, third, or fourth point to a string value and the AboutVector3d() function for converting the coordinates of the normal to a string value. The AboutPoint3d() and AboutVector3d() functions require an instance of the three-dimensional point or three-dimensional vector to be converted as an argument and return a string containing the X,Y,Z coordinates.

The PrintTraceProperties() function has the following implementation:


void PrintTraceProperties(OdDbTrace* pTrace)
{
  OdGePoint3d point;

  pTrace->getPointAt(0, point);
  wcout << L"\n  1th point = " << AboutPoint3d(point);
  pTrace->getPointAt(1, point);
  wcout << L"\n  2th point = " << AboutPoint3d(point);
  pTrace->getPointAt(2, point);
  wcout << L"\n  3th point = " << AboutPoint3d(point);
  pTrace->getPointAt(3, point);
  wcout << L"\n  4th point = " << AboutPoint3d(point);

  wcout << L"\n  Normal = " << AboutVector3d(pTrace->normal());
  wcout << L"\n  Thickness = " << pTrace->thickness();
}

The ModifyTraceProperties() function requires a pointer to an existing trace object and implements the console menu for the listed operations. The function organizes a loop that inquires about the operation code and uses the switch statement to select whether the case must be performed. The function processes user actions in the loop until the user selects the [Q] operation. The function uses EntryPoint3d() function for entering coordinates of a three-dimensional point and the EntryVector3d() function for entering coordinates of a three-dimensional vector. The EntryPoint3d() and EntryVector3d() functions require a reference to an instance of the three-dimensional point or three-dimensional vector to be modified as an argument and return True when the passed instance is modified successfully or False when the user cancels entry.

When the user selects [P], the function displays the common object properties using the PrintObjectProperties() function, general entity properties using the PrintEntityProperties() function, and specific trace properties using the PrintTraceProperties() function. When the user selects the [p] operation, the function displays only specific trace properties.

When the user selects [1], [2], [3], or [4], the function calculates the point index as the difference between the obtained letter code and code of the '1' letter, gets the coordinates of the point corresponding to the calculated index using the getPointAt() method, calls the EntryPoint3d() function for entry of its coordinates, and sets the returned point as the new trace point using the setPointAt() method if entry is successful.

When the user selects [N], the function gets the normal to the plane of the trace using the normal() method, calls the EntryVector3d() function for entry of its coordinates, and sets the returned vector as a new normal using the setNormal() method if entry is successful.

When the user selects [T], the function gets the current thickness using the thickness() method and requests a double value. If an entered value is incorrect, the function displays an error message and breaks to the console menu. If an entered value is correct, the function sets the new thickness using the setThickness() method.

When the user selects [M], the function calls the ModifyEntityProperties() function and passes to it the pointer to the trace object for modifying its general entity properties. After modifying, the function displays all trace properties using the [P] operation.

When the user selects [Q], the function ends the loop and returns in the calling function.

The ModifyTraceProperties() function has the following implementation:


#include "..\Common\DemoMain.h"
#include "DbTrace.h"
#include <iostream>
using namespace std;

OdString AboutPoint3d(const OdGePoint3d& point);
bool EntryPoint3d(OdGePoint3d& point);

OdString AboutVector3d(const OdGeVector3d& vector);
bool EntryVector3d(OdGeVector3d& vector);

void PrintEntityProperties(OdDbEntity *pEntity);
void ModifyEntityProperties(OdDbEntity *pEntity);

void ModifyTraceProperties(OdDbTrace* pTrace)
{
  wchar_t ch = L'\0';
  OdInt16 index;
  double value;
  OdGePoint3d point;
  OdGeVector3d vector;

  wcout << L"\n\nStart modifying of the trace properties";

  do {
    switch(ch)
    {
      case L'M':          // Modify the general properties
      case L'm':
        ModifyEntityProperties(pTrace);


      case L'P':          // Print the whole properties
        wcout << L"\nH=" << pTrace->handle().ascii();
        wcout << L"\n  ------ Common Object Properties";
        PrintObjectProperties(pTrace);
        wcout << L"\n  ------ General Entity Properties";
        PrintEntityProperties(pTrace);


      case L'p':          // Print the specific properties
        wcout << L"\n  ------ Specific Trace Properties";
        PrintTraceProperties(pTrace);
        wcout << L"\n";        
        break;


      case L'N':          // Set the normal
      case L'n':
        vector = pTrace->normal();
        if(EntryVector3d(vector))
        {
          pTrace->setNormal(vector);
          wcout << L"Normal is set to " << AboutVector3d(pTrace->normal()) << L"\n";
        }
        break;


      case L'T':          // Set the thickness
      case L't':
        wcout << L"\nCurrent thickness = " << pTrace->thickness()
              << L"\nEntry a new thickness [t>0-along normal|t<0-opposite normal]:>";
        wcin >> value;

        if(!wcin.fail() && wcin.peek() == 10)
        {
            pTrace->setThickness(value);
            wcout << L"Thickness is set to: " << pTrace->thickness() << L"\n";
        }
        else { wcin.clear();  wcin.sync();  wcout << L"Error: Invalid entered value\n"; }
        break;


      case L'1':          // Set the vertex point
      case L'2':
      case L'3':
      case L'4':
        index = (OdInt16)(ch - L'1');
        pTrace->getPointAt(index, point);

        wcout << L"Point(" << index + 1 << L")";
        if(EntryPoint3d(point))
          pTrace->setPointAt(index, point);
        break;


      case L'\0':         // Skip an action
        break;

      default:
        wcout << L"Error: Incorrect operation\n";
    }
    wcout << L"\nN. Set the normal";
    wcout << L"\nT. Set the thickness";
    wcout << L"\n1,2,3,4. Set the vertex point";
    wcout << L"\nP. Print the whole properties";
    wcout << L"\np. Print the specific properties";
    wcout << L"\nM. Modify the general properties";
    wcout << L"\nQ. Quit";
    wcout << L"\nSelect operation:>";
    wcin >> ch;
  }
  while(ch != L'Q' && ch != L'q');

  wcout << L"Stop modifying of the trace properties\n";
}

Testing gives the following results:


Start modifying of the trace properties

N. Set the normal
T. Set the thickness
1,2,3,4. Set the vertex point
P. Print the whole properties
p. Print the specific properties
M. Modify the general properties
Q. Quit
Select operation:>

When the operation is p-"Print the specific properties":


------ Specific Trace Properties
1th point = (0,0,0)
2th point = (0,0,0)
3th point = (0,0,0)
4th point = (0,0,0)
Normal = (0,0,1)
Thickness = 0

When the operation is 1-"Set the first point":


Point(1)
Current coordinates: (0,0,0)
Entry X-coordinate:>1.1
Entry Y-coordinate:>2.2
Entry Z-coordinate:>0

When the operation is 2-"Set the second point":


Point(2)
Current coordinates: (0,0,0)
Entry X-coordinate:>4.2
Entry Y-coordinate:>6.1
Entry Z-coordinate:>0

When the operation is 3-"Set the third point":


Point(3)
Current coordinates: (0,0,0)
Entry X-coordinate:>9.6
Entry Y-coordinate:>9.1
Entry Z-coordinate:>0

When the operation is 4-"Set the fourth point":


Point(4)
Current coordinates: (0,0,0)
Entry X-coordinate:>6.5
Entry Y-coordinate:>4.1
Entry Z-coordinate:>0

When the operation is N-"Set the normal":


Current vector directions: (0,0,1)
Entry X-direction:>0.9
Entry Y-direction:>0.1
Entry Z-direction:>1.2
Normal is set to (0.598671,0.066519,0.798228)

When the operation is T-"Set the thickness":


Current thickness = 0
Entry a new thickness [t>0-along normal|t<0-opposite normal]:>2.1
Thickness is set to: 2.1

When the operation is P-"Print all properties":


H=40
  ------ Common Object Properties
  Type: AcDbTrace
  Status: unerased,resident,modified,forRead,forWrite
  Database: c:\test.dwg
  ------ General Entity Properties
  Block: [1F-"*Model_Space"-Block]
  Layer: [10-"0"-Layer]
  Material: [3C-"ByLayer"-Material]
  Linetype: [15-"ByLayer"-Linetype]
  Lineweight: [kLnWtByLayer]
  Transparency: [byLayer]
  Visual style: [Db:kNull]
  PlotStyleName: ByLayer
  Linetype scale: 1
  Color: [byLayer], Index = 256
  State: Planar, Visible, Cast Shadows, Receive Shadows
  ------ Specific Trace Properties
  1th point = (1.1,2.2,0)
  2th point = (4.2,6.1,0)
  3th point = (9.6,9.1,0)
  4th point = (6.5,4.1,0)
  Normal = (0.598671,0.066519,0.798228)
  Thickness = 2.1

Use the Q-"Quit" operation to exit:


Stop modifying of the trace properties

See Also

Working with Traces

Example of Entering and Displaying for 3D Point Objects

Example of Entering and Displaying for 3D Vector Objects

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.