Color Attribute Implementation File
List of: Discussion Topic
Subjects: Attributes
Contents: Kernel

To create a color attribute implementation (color_attrib.cxx) file:


1. Include necessary system header files.


2. Include the header file declaring this specific attribute.


3. Include the header files necessary to define new entities.


4. Define THIS, THIS_LIB, PARENT, and PARENT_LIB macros to make subsequent definitions of this attribute and its parent easier.


5. Define the identifier used externally to identify a particular entity type.


6. Call macros used to implement the standard attribute methods and data. (These macros are discussed elsewhere in this chapter.)


7. Write any additional functions that were declared in the .hxx file.



a. Define the constructor. The example calls the ATTRIB constructor to set the owning entity pointer then sets the member data.



b. Define the function to set the member data.



c. Define the virtual function called when the owner entity splits. The new face has no color, so the color attribute is duplicated onto the new face.



d. Define the virtual function called when two entities merge. In the merge function, several possibilities exist. If the two entities have the same color, the result has that color. If the entities have different colors, the result takes the color of the owning entity not deleted. If only the deleted entity has a color, that color is assigned to the result.

Example 3-6 shows what what a color attribute implementation file (color_attrib.cxx) could look like.

C++ Example

#include <stdio.h>
#include <memory.h>
#include "kernel/acis.hxx"
#include "kernel/kerndata/data/datamsc.hxx"
#include "color_attrib.hxx"

// Implementation of color attribute. This is attached to body,
// face or edge objects, and is used to demonstrate attribute
// migration

// Define macros for this attribute and its parent, to simplify
// later stuff and make it suitable for making into macros.
#define THIS() ATTRIB_COL
#define THIS_LIB NONE
#define PARENT() ATTRIB_ABC
#define PARENT_LIB NONE

// Identifier used externally to identify a particular entity
// type. This is used in the save/restore system for translating
// to/from external file format.
#define ATTRIB_COL_NAME "color"

// Implement the standard attribute functions.
ATTRIB_DEF("color_attribute")


// Define color names for printout

static char* col_name[] = {


"black",


"red",


"green",


"blue",


"cyan",


"yellow",


"magenta",


"white"

};

debug_string("color", col_name [color_data], fp);

SAVE_DEF

write_int(color_data);

// Save specific data

RESTORE_DEF

set_color(read_int());

COPY_DEF

set_color(from->color());

SCAN_DEF

// (no specific pointer data)

FIX_POINTER_DEF

// (no specific pointer data)

TERMINATE_DEF

// make a color attribute
ATTRIB_COL::ATTRIB_COL(

ENTITY* owner,

int col

) : ATTRIB_ABC(owner)

{

// Initialize members.

color_data = col; }

// Set the member data.
void ATTRIB_COL::set_color(

int new_col

)
{

backup();

color_data = new_col;
}

// Virtual function called when an owner entity is being split.
void ATTRIB_COL::split_owner(

ENTITY* new_ent

)
{

// Duplicate the attribute on the new entity

new ATTRIB_COL(new_ent, color_data);
}

// Virtual function called when two entities are to be merged.
void ATTRIB_COL::merge_owner(

ENTITY* other_ent,

logical delete_owner

)
{

// If the owner of this attribute is going to be deleted, and

// there is no color attached to the other entity, then we

// transfer to that other entity.

if (delete_owner) {


ATTRIB* other_att = find_attrib(



other_ent,



ATTRIB_ABC_TYPE,



ATTRIB_COL_TYPE


);


if(other_att == NULL) {



// No color on other entity, so transfer ourselves



move(other_ent);


}

}
}

Example 3-6. color_attrib.cxx Color Attribute Implementation File
PDF/KERN/03ATT.PDF
HTM/DATA/KERN/KERN/03ATT/0018.HTM