Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

programming question


Recommended Posts

Hi, I'm new to mastercam and I was wondering if you guys could help me out. I'm trying to write a program to rotate a part based on parameters read in from a file. Is it possible to do this with C# or vbs, or even with c-hooks? Any pointers in the right direction would be appreciated. Thanks in advance for your help.

Link to comment
Share on other sites

jadams,

 

Welcome to the Forum

 

cheers.gif

 

Best forum on the 'net

 

What is it you are trying to accomplish with the rotate? Are you just trying to move the part origin or are you trying to program toolpaths?

Link to comment
Share on other sites

Eventually, my goal is to be able to take in a part and rotate/translate it to a given orientation so another bit of code can plan toolpaths based on that orientation. Are there any bits of example code out there that show how to perform geometry operations or something similar?

Link to comment
Share on other sites

quote:

Are there any bits of example code out there that show how to perform geometry operations or something similar?


Here's an example of how I rotate all entities 90 degrees about the origin:

 

code:

db_ptr_type ptr, e_ptr;

ent entity;

boolean bSuccf;

p_3d ptOrigin;

 

 

// init the origin

ptOrigin[X] = ptOrigin[Y] = ptOrigin[Z] = 0;

 

// rewind the database back to the beginning

ptr = db_start;

 

// traverse the entire database

for ( ; ; )

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

if (!bSuccf) break; // bail if there aren't anymore entities

 

// rotate the entity 90 degrees about the origin in the Top view

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bSuccf);

}

 

// refresh the view

refresh_view_menu ();

 

// rebuild graphics

rebuild_graphics();

rotate_silent() is defined in m_math.h in the V9 SDK and in XForm_CH.h in the X SDK.

 

HTH

Link to comment
Share on other sites

Odd. The last parameter of get_ent() is a reference to a boolean variable. When get_ent() is able to retrieve another entity from the database it returns TRUE. If it can't, it returns FALSE. So our main concern should be the value of bSuccf referenced by get_ent() after each iteration of the loop. The loop can be written like:

 

code:

for ( ; ; )

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

if (!bSuccf) break; // bail if there aren't anymore entities

 

// rotate the entity 90 degrees about the origin in the Top view

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bSuccf);

}

Or like this:

 

code:

while (1)

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

if (!bSuccf) break; // bail if there aren't anymore entities

 

// rotate the entity 90 degrees about the origin in the Top view

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bSuccf);

}

Or like this:

 

code:

bSuccf = TRUE;

 

while (bSuccf)

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

// rotate the entity 90 degrees about the origin in the Top view

if (bSuccf)

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bRotSuccf);

}

And then some. They all accomplish the same thing. If you test the value of bSuccf, is it always true?

Link to comment
Share on other sites

Thanks for the quick response. bSuccf is always true. I had to change it from a boolean to a MC_BOOL type to get the code to compile, but I wouldn't think this would have any affect? Here's what I have for the loop now:

 

code:

while (bSuccf){    

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

if (!(bSuccf == true)) { ::MessageBox(NULL, "bSuccf is false", "Project1", MB_OK); break;}

 

// rotate the entity 90 degrees about the origin in the Top view

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bSuccf);

}

Link to comment
Share on other sites

How about:

 

code:

while (bSuccf){    

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

if (!(bSuccf == TRUE)) { ::MessageBox(NULL, "bSuccf is false", "Project1", MB_OK); break;}

 

// rotate the entity 90 degrees about the origin in the Top view

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bSuccf);

}

Link to comment
Share on other sites

Since you're using the same variable to get the success flag from both get_ent() and rotate_silent(), your loop could be continuing since rotate_silent() could very well continue to rotate the last entity. Maybe try a separate MC_BOOL for the rotation:

 

code:

// declare the success flags

MC_BOOL bSuccf, bRotSuccf;

 

 

// init the flags to TRUE

bSuccf = bRotSuccf = TRUE;

 

ptr = db_start;

 

while (bSuccf)

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

// rotate the entity 90 degrees about the origin in the Top view

if (bSuccf)

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bRotSuccf);

}

Link to comment
Share on other sites

Poor man's debugging style:

 

code:

db_ptr_type ptr, e_ptr;

ent entity;

// declare the success flags

MC_BOOL bSuccf, bRotSuccf;

 

 

// init the flags to TRUE

bSuccf = bRotSuccf = TRUE;

 

ptr = db_start;

 

while (bSuccf)

{

// get the current entity in the database

get_ent(&ptr, &e_ptr, &entity, ALIVE_BIT,

P_ID|L_ID|A_ID|S_ID|SURF_ID|SOLID_ID|SPLINE_ID|N_ID|W_ID|D_ID,

&bSuccf);

 

// rotate the entity 90 degrees about the origin in the Top view

if (bSuccf)

{

AfxMessageBox("We were able to get an entity. Now we'll rotate it.");

rotate_silent(&e_ptr, TRUE, FALSE, ptOrigin, 90.0, 1, &bRotSuccf);

}

else

AfxMessageBox("We couldn't get an entity. Our loop should be terminating now.");

}

How many times do you see "We were able to get an entity. Now we'll rotate it." and "We couldn't get an entity. Our loop should be terminating now."? The number of times that you see the first message should be the same as the number of alive entities in the currently opened file. You should only see the second message once.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...