//////////////////////////////////////////////////////////////////////
// Code Example:
//
//////////////////////////////////////////////////////////////////////
// This function is passed the step number. This allows for an application to
// produce code with several steps as demonstrated.
// Return 1 if you have finished, 0 if otherwise and -1 to exit.
int CMyCode::RunMyCode(int step)
{
// The code below is sample code. On exiting the function the bodies that
// exist will be drawn
int finished = 0;
int i = 0;
char message[500] = "";
//Partition and Memory Blocks///////////
int n_partitions = 0;
static PK_PARTITION_t *partitions = NULL;
static PK_PARTITION_t partition, new_partition = NULL;
static PK_MEMORY_block_t block = {NULL, 0, NULL};
static PK_MEMORY_block_t deltas = {NULL, 0, NULL};
PK_LOGICAL_t current = false;
////////////////////////////////////////
//For file write/read examples////////
PK_PARTITION_transmit_o_t transmit_opts;
PK_PARTITION_receive_o_t receive_opts;
/////////////////////////////////////
//For moving PMARKs///////////////////
int n_new, n_mod, n_del;
static PK_PMARK_t startpmark, pmark1, pmark2, * badmark = NULL;
PK_PMARK_t pmarks[2];
int n_bad_marks, n_following = 0;
PK_PARTITION_delete_o_t delete_opts;
//////////////////////////////////////
//Part Creation Variables/////////////
static PK_BODY_t cube, cyl, body, body2 = NULL;
PK_AXIS2_sf_t basis_set;
PK_VECTOR_t loc1 = {-10, 0, 0};
PK_VECTOR1_t axis1 = {0.577350269189626, 0.577350269189626, 0.577350269189626};
PK_VECTOR1_t ref_dir1 = {-0.408248290463863, -0.408248290463863, 0.816496580927726};
PK_VECTOR_t loc2 = {10, 0, 0};
//////////////////////////////////////
//Colouring///////////////////////////
void ColourFaces(PK_BODY_t body, double colour[3]);
void ColourEdges(PK_BODY_t body, double colour[3]);
void ColourBody(PK_BODY_t body, double colour[3]);
double red[3]={1,0,0};
double green[3]={0,1,0};
double purple[3]={0.5,0,1};
double yellow[3]={1,1,0};
double cyan[3]={0,1,1};
double aqua[3]={0.2,0.8,0.8};
double pink[3]={1,0.2,0.5};
//////////////////////////////////////
switch( step )
{
case 1:
//Create a starting PMARK and a block.
//Find the tag of the partition of interest and remove other partitions.
PK_SESSION_ask_partitions(&n_partitions, &partitions);
partition = partitions[0];
if(n_partitions>0)
{
PK_MEMORY_free(partitions);
}
//Make the starting PMARK so that the example can be rolled to a clean state.
//Note that PK_PARTITION_make_pmark is a deprecated function. Any new code should use the function that has superseded it.
PK_PARTITION_make_pmark(partition, &startpmark);
//Make a block.
//Set up basis set for block.
//Create and colour.
basis_set.location = loc1;
basis_set.axis = axis1;
basis_set.ref_direction = ref_dir1;
PK_BODY_create_solid_block(10, 2, 10, &basis_set, &cube);
ColourBody(cube, green);
CExampleAppDoc::ExAppSetStatusBarString("1.) Create a PMARK for the empty session. Create a block.");
break;
case 2:
//Create a PMARK for the block and create a cylinder.
//PMARK for block.
//Note that PK_PARTITION_make_pmark is a deprecated function. Any new code should use the function that has superseded it.
PK_PARTITION_make_pmark(partition, &pmark1);
//Create and colour cylinder.
basis_set.location = loc1;
basis_set.axis = axis1;
basis_set.ref_direction = ref_dir1;
PK_BODY_create_solid_cyl(10, 2, &basis_set, &cyl);
ColourBody(cyl, purple);
//PMARK for cylinder and block.
PK_PARTITION_make_pmark(partition, &pmark2);
CExampleAppDoc::ExAppSetStatusBarString("2.) Create a PMARK for the block, create a cylinder and then another PMARK for both bodies.");
break;
case 3:
//Transmit the partitions without and with deltas.
//Initialise options.
PK_PARTITION_transmit_o_m(transmit_opts);
//Transmit the session without deltas.
PK_PARTITION_transmit(partition, "../Example parts/Partition_no_deltas", &transmit_opts);
//Set up a partition transmit with deltas.
transmit_opts.transmit_deltas = PK_PARTITION_xmt_deltas_main_c;
//Have to go to a PMARK to transmit deltas. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_goto(pmark2, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
PK_PARTITION_transmit(partition, "../Example parts/Partition_with_deltas", &transmit_opts);
CExampleAppDoc::ExAppSetStatusBarString("3.) Transmit the partition without deltas. Roll to the second pmark and transmit with deltas.");
break;
case 4:
//Reset the partition.
//Reset the partition by returning to the starting PMARK. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_goto(startpmark, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
//Delete PMARKS 1 & 2.
pmarks[0] = pmark1;
pmarks[1] = pmark2;
PK_PMARK_delete(2, pmarks, &n_bad_marks, &badmark);
CExampleAppDoc::ExAppSetStatusBarString("4.) Return to the start pmark. Delete PMARKS 1 & 2");
break;
case 5:
//Load in the session transmited earlier which does not contain deltas.
//Set options and receive partition (no deltas).
PK_PARTITION_receive_o_m(receive_opts);
PK_PARTITION_receive("../Example Parts/Partition_no_deltas", &receive_opts, &new_partition);
//Move to the newly loaded partition
PK_PARTITION_set_current(new_partition);
//Delete the original partition.
PK_PARTITION_delete_o_m(delete_opts);
PK_PARTITION_delete(partition, &delete_opts);
//Set partition.
partition = new_partition;
CExampleAppDoc::ExAppSetStatusBarString("5.) Load in partition with no deltas. Remove old partition.");
break;
case 6:
/*Show that the partition can not be moved to another
PMARK due to lack of deltas. */
CExampleAppDoc::ExAppSetStatusBarString("6.) Cannot find other PMARKS due to the lack of deltas.");
//Find current PMARK. Look for preceding and following PMARKS.
PK_PARTITION_ask_pmark(partition, &pmark2, ¤t);
PK_PMARK_ask_preceding(pmark2, &pmark1); //returns null for pmark1.
PK_PMARK_ask_following(pmark2, &n_following, NULL); //n_following comes back as 0.
//Display a message box to show that there are no deltas to other PMARKS.
if(pmark1 == 0)
{
sprintf_s(message, BUFFER_SIZE, "The number of preceding PMARKs is: 0 \n"
"The number of following PMARKs is: %d", n_following);
CExampleAppDoc::ExAppShowMessage(message);
}
break;
case 7:
//Load in the partition which uses deltas.
//Set options and receive partition.
PK_PARTITION_receive_o_m(receive_opts);
receive_opts.receive_deltas = PK_PARTITION_rcv_deltas_yes_c;
PK_PARTITION_receive("../Example Parts/Partition_with_deltas", &receive_opts, &new_partition);
//Move to the newly loaded partition
PK_PARTITION_set_current(new_partition);
//Delete the original partition.
PK_PARTITION_delete_o_m(delete_opts);
delete_opts.delete_non_empty = PK_LOGICAL_true;
PK_PARTITION_delete(partition, &delete_opts);
//Set partition.
partition = new_partition;
CExampleAppDoc::ExAppSetStatusBarString("7.) Load in partition with deltas. Remove old partition.");
break;
case 8:
//Find the latest PMARK. Find the preceeding PMARK and roll to it.
//Find the latest PMARK.
PK_PARTITION_ask_pmark(partition, &pmark2, ¤t);
//Find preceeding PMARK.
PK_PMARK_ask_preceding(pmark2, &pmark1);
//Roll back to pmark1. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_goto(pmark1, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
CExampleAppDoc::ExAppSetStatusBarString("8.) Find previous PMARK and roll to it.");
break;
case 9:
/*Show that the deltas have loaded in by finding out if a pmark follows
pmark1*/
CExampleAppDoc::ExAppSetStatusBarString("9.) Use deltas to find the start PMARK. Roll to it. Delete PMARKS 1&2.");
//Look for following PMARK.
PK_PMARK_ask_following(pmark1, &n_following, NULL);
//Display a message box to show that there are deltas to other PMARKS.
sprintf_s(message, BUFFER_SIZE, "The number of PMARKs following pmark1 is: %d", n_following);
CExampleAppDoc::ExAppShowMessage(message);
//Find the start pmark from deltas.
PK_PMARK_ask_preceding(pmark1, &startpmark);
//Roll back to start pmark using loaded deltas. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_goto(startpmark, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
//Delete PMARKS 1 & 2.
pmarks[0] = pmark1;
pmarks[1] = pmark2;
PK_PMARK_delete(2, pmarks, &n_bad_marks, &badmark);
break;
case 10:
//Set up basis set for block.
basis_set.location = loc1;
basis_set.axis = axis1;
basis_set.ref_direction = ref_dir1;
//Create block.
PK_BODY_create_solid_block(10, 10, 10, &basis_set, &body);
//Colour block.
ColourBody(body, cyan);
CExampleAppDoc::ExAppSetStatusBarString("10.) Create a new block.");
break;
case 11:
//Make a pmark so there is some information in the deltas for later.
//Note that PK_PARTITION_make_pmark is a deprecated function. Any new code should use the function that has superseded it.
PK_PARTITION_make_pmark(partition, &pmark1);
//Set up basis set for a second block.
basis_set.location = loc2;
basis_set.axis = axis1;
basis_set.ref_direction = ref_dir1;
//Create and colour another block.
PK_BODY_create_solid_block(5,5,5, &basis_set, &body2);
ColourBody(body2, pink);
CExampleAppDoc::ExAppSetStatusBarString("11.) Create a pmark and a second block.");
break;
case 12:
//Make another mark.
//Note that PK_PARTITION_make_pmark is a deprecated function. Any new code should use the function that has superseded it.
PK_PARTITION_make_pmark(partition, &pmark2);
//Set up transmit options.
PK_PARTITION_transmit_o_t partition_opts;
PK_PARTITION_transmit_o_m(partition_opts);
//Set the option to transmit the deltas.
partition_opts.transmit_deltas = PK_PARTITION_xmt_deltas_all_c;
//Transmit the partition and deltas.
PK_PARTITION_transmit_b(partition, &partition_opts, &block, &deltas);
CExampleAppDoc::ExAppSetStatusBarString("12.) Make another pmark and transmit the partition and deltas to memory blocks.");
break;
case 13:
//Return to start PMARK. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_goto(startpmark, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
//Delete PMARKS 1 & 2.
pmarks[0] = pmark1;
pmarks[1] = pmark2;
PK_PMARK_delete(2, pmarks, &n_bad_marks, &badmark);
CExampleAppDoc::ExAppSetStatusBarString("13.) Return to start PMARK. Delete PMARKS 1&2.");
break;
case 14:
//Load the partitions and deltas back into the session.
//Set up receive options.
PK_PARTITION_receive_o_t receive_opts;
PK_PARTITION_receive_o_m(receive_opts);
//Set the option to receive deltas.
receive_opts.receive_deltas = PK_PARTITION_rcv_deltas_yes_c;
receive_opts.deltas_block = &deltas;
//Receive the partion and deltas.
PK_PARTITION_receive_b(block, &receive_opts, &new_partition);
//Free the memory blocks for the block and deltas.
PK_MEMORY_block_f(&block);
PK_MEMORY_block_f(&deltas);
CExampleAppDoc::ExAppSetStatusBarString("14.) Load the partition and deltas back in.");
break;
case 15:
//Roll back to the first pmark to just show the cyan cube.
//Move to the newly loaded partition
PK_PARTITION_set_current(new_partition);
//Delete the original partition.
PK_PARTITION_delete_o_m(delete_opts);
PK_PARTITION_delete(partition, &delete_opts);
//Set partition.
partition = new_partition;
//Move to last PMARK, check it is then the current PMARK. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PARTITION_ask_pmark(partition, &pmark2, ¤t); //pmark2 is not current, so roll to it.
PK_PMARK_goto(pmark2, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
//Find and go to previous mark.
PK_PMARK_ask_preceding(pmark2, &pmark1);
PK_PMARK_goto(pmark1, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
CExampleAppDoc::ExAppSetStatusBarString("15.) Use the delta in the memory block to roll back to the previous PMARK.");
break;
case 16:
//Reset the session.
//Return to start PMARK. Note that PK_PMARK_goto is a deprecated function. Any new code should use the function that has superseded it.
PK_PMARK_ask_preceding(pmark1, &startpmark);
PK_PMARK_goto(startpmark, &n_new, NULL, &n_mod, NULL, &n_del, NULL);
//Delete PMARKS 1 & 2.
pmarks[0] = pmark1;
pmarks[1] = pmark2;
PK_PMARK_delete(2, pmarks, &n_bad_marks, &badmark);
CExampleAppDoc::ExAppSetStatusBarString("16.) Return to the start pmark. Delete PMARKS 1 & 2.");
break;
default:
finished = 1;
}
return finished;
}
void ColourFaces(PK_BODY_t body, double colour[3])
{
/* Colours a given body using the colour specified by the input.
Creates an attribute, locates all the faces on the body and then
applies the attribute.*/
PK_ATTDEF_t colour_attdef = 0;
PK_ATTRIB_t colour_attrib = 0;
PK_FACE_t *faces = NULL;
int n_faces=0, n_attribs = 0;
int i = 0;
PK_BODY_ask_faces(body, &n_faces, &faces); //locate the faces.
PK_ATTDEF_find("SDL/TYSA_COLOUR", &colour_attdef);
//Colour all of the faces on the cube.
for(i=0; i 0)
{
//Delete any exisiting colour attribs.
PK_ENTITY_delete_attribs(faces[i], colour_attdef, &n_attribs);
}
PK_ATTRIB_create_empty(faces[i], colour_attdef, &colour_attrib);
PK_ATTRIB_set_doubles(colour_attrib, 0, 3, colour);
}
PK_MEMORY_free(faces);
}
void ColourEdges(PK_BODY_t body, double colour[3])
{
/* Colours a given body using the colour specified by the input.
Creates an attribute, locates all the faces on the body and then
applies the attribute.*/
PK_ATTDEF_t colour_attdef = 0;
PK_ATTRIB_t colour_attrib = 0;
PK_EDGE_t *edges = NULL;
int n_edges = 0;
int i = 0, n_attribs = 0;
PK_BODY_ask_edges(body, &n_edges, &edges); //locate the faces.
PK_ATTDEF_find("SDL/TYSA_COLOUR", &colour_attdef);
//Colour all of the faces on the cube.
for(i=0; i 0)
{
PK_ENTITY_delete_attribs(edges[i], colour_attdef, &n_attribs);
}
PK_ATTRIB_create_empty(edges[i], colour_attdef, &colour_attrib);
PK_ATTRIB_set_doubles(colour_attrib, 0, 3, colour);
}
//Free memory used for edges.
PK_MEMORY_free(edges);
}
void ColourBody(PK_BODY_t body, double colour[3])
{
ColourFaces(body, colour);
ColourEdges(body, colour);
}