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:

Pointer to Structure


Recommended Posts

Hello All.

 

I'm using the help file on "constr_circle". It says that it is reconstructing it. I'd simply like to construct a circle. Is there a way to do that given a_2d parameters.

 

I can't get constr_circle to work. I defined my a_2d structure, but inside the constr_circle function call I'm not sure how to pass in the "arc" structure.

 

I define my a_2d structure as:

 

a_2d *arc;

 

Then I can refer to members of *arc with:

 

(*arc).r = 0.5;

 

But when I call constr_circle I use this:

 

constr_circle(end_pt, c, cw, min_radius, arc, succf);

 

And an error:

 

C2664: 'constr_circle' : cannot convert parameter 5 from 'a_2d *' to 'a_2d *'

 

Any help for a newbie with calling a structure would be appreciated.

Link to comment
Share on other sites

Be careful, unless you have some allocation happening between

 

a_2d *arc;

 

and

 

(*arc).r = 0.5;

 

you will corrupt application's memory. You probably want to change the first line to

 

a_2d arc;

 

and call the funnction with like this

 

constr_circle(end_pt, c, cw, min_radius, &arc, &succf);

 

Notice that I have fixed "succf" argument too, so just be sure you declare it as

 

MC_BOOL succf;

 

Now back to the message, I think you did not type it correctly. It says that it can not covert the argument from one type to another, where both types are the same! Can you just "cut & paste" it to avoid mistakes?

 

Marko

Link to comment
Share on other sites

Actually I did cut and paste my code from visual studio. If you mean the error, I also copy and pasted that. I did this because I had the same disbelief that you must be having when I saw the error!

 

I also thought the error code C2664 might be helpful.

 

Thanks for the suggestion on using a reference operator, but I had tried that too. With that attempt I get this error (notice again the error code C2664):

 

error C2664: 'constr_circle' : cannot convert parameter 5 from 'a_2d *__w64 ' to 'a_2d *'

 

Any thoughts on that?

Link to comment
Share on other sites

The arc structure is an out parameter. The function constr_circle fills it in.

 

code:

void MakeCircle()

{

// Parameters to pass to 'constr_circle'

p_2d end_pt;

p_2d center;

MC_BOOL cw;

real min_radius;

a_2d arc; // pass the address of this (&)

MC_BOOL succf; // pass the address of this (&)

 

// Data for the new circle

end_pt[X] = 1.0;

end_pt[Y] = 1.0;

center[X] = 0.0;

center[Y] = 0.0;

cw = TRUE;

min_radius = nc_tol;

constr_circle (end_pt, center, cw, min_radius, &arc, &succf);

// If (succf == TRUE), arc is now "filled" with the data of the new arc.

}

Be sure to note that will you not "see" this arc on the graphics screen - as this function does not add this arc (as an entity) to the part database.

Link to comment
Share on other sites

arenner,

 

I haven't had a chance to try this, so I'll just fly some things for you to try.

 

1. Programming tip:

code:

(*arc).r

is bad form. It's technically correct, just "bad" as in somewhat naughty. Better would be to see

code:

arc->r

it's cleaner. This is especially true when you have something like (*(*(*a).B).c).d, where it would be better to write a->b->c->d. It means the same thing.

 

2. As markov said:

quote:

You probably want to change the first line to

a_2d arc;

 

and call the funnction with like this

 

constr_circle(end_pt, c, cw, min_radius, &arc, &succf);


He's spot on about the memory corruption also. Pointers have to point somewhere! Whenever you decalare a pointer, make it point, even if you're just setting it to null, as in:

a_2d * arc = NULL;

 

or allocate the memory right then and there with:

a_2d * arc = new a_2d;

 

Remember to free any memory you allocate:

code:

delete arc;

Anyways, the reason the function takes a pointer is that it is using an older C-style method of passing called "pass by pointer" where you pass the address of the variable who will hold the value. This is probably one of the older functions in the system.

 

Tutorial here:

Pass by pointer

 

Ignore the author's comments about preceding the name with a "p." It's not universally accepted, though I know some people do do that. It isn't bad form, just not necessarily best practice.

Link to comment
Share on other sites

First, let me say thanks for the advice, but I'm still not getting very far here. As Roger said arc is being sent, not input. So I am reading Jon's comments with that in mind. Now it makes a lot more sense. I was declaring and defining the a_2d structure called arc in my code. I'm guessing that I don't even have to do that. It will simply output an a_2d structure. Reading the help didn't make that very clear. Why does the help say reconstruct?

 

I'm reading Jon's comments, and yes, I did change the '.' to a '->'. But again, there's no need for that if arc is not an input. I don't need to define each element of the arc structure.

 

Also, I'm reading min_radius as the radius of the arc, so I thought end_pt was redundant. But then seeing Roger's line 'min_radius = nc_tol;' made me think otherwise. It looks like you set min_radius to the smallest nc tolerance setting. Is min_radius or end_pt defining my circle's radius.

 

To Roger's comment that it isn't constructed on the graphics screen, or in the database, makes this function seem useless. What is this function used for/where's the circle? I am drawing these circles so that I can later extrude them into cylinders. I'll need them to at least be in a database so that I can refer to them. Is there a different way to draw circles?

Link to comment
Share on other sites

renner,

 

constr_circle constructs a full circle based on the center_pt and end_pt, which determines the radius.

'min_radius' is exactly that, the minimum radius you will acccept being created.

 

 

If you want to create entities "in the Mastercam database", you can use store_ent

 

Below is a sample of this...

 

code:

//Required includes ->

#include "3dvars_ch.h"

#include "dbloio_ch.h"

 

//-----------------------------------------

// Construct an Arc entity in the database

//-----------------------------------------

void make_arc (

p_3d c, // In: arc center

real r, // In: arc radius

real sa, // In: start angle

real sw, // In: sweep

byte color, // In: color

long level, // In: level

attributes attrib, // In: construction attributes

MC_BOOL *succf, // Out: FALSE = error saving entity in the database

DB_LIST_ENT_PTR &d_ptr // Out: Misc. database pointers

)

 

{

ent entity; // entity struct to be stored

a_3d arc; // The 'arc' data of the entity to be stored

 

entity.id = A_ID; // The 'ent' will be an Arc

entity.refs = 0;

// Copy the passed parameters into the 'a_3d' structure

copy_p_3d (arc.c, c);

arc.r = r;

arc.sa = sa;

arc.sw = fabs (sw);

arc.view = max (constr_view_n, 1);

entity.u.ar = arc;

store_ent (&entity, &d_ptr, 0, color, level, attrib, succf); // 'store_ent' is declared in "DbLoIo_CH.h"

}

 

 

 

// This code used "elsewhere" in the project...

 

MC_BOOL succf;

DB_LIST_ENT_PTR db_ptr = NULL; // Database pointer (to the new Arc)

p_3d ctr_pt; // Arc centerpoint

ctr_pt[X] = 0,0;

ctr_pt[Y] = 0,0;

if (constr_view_is_3d)

{ ctr_pt[Z] = constr_depth; }

else

{ ctr_pt[Z] = 0.0; }

real arc_radius = 1.0;

real start_angle = 0.0;

real end_angle = TWO_PI; // 'TWO_PI' is declared in "3DVARS_ch.h"

 

 

// Call our 'make_arc' function which uses 'store_ent' to put an arc into the database

make_arc(ctr_pt, arc_radius, start_angle, end_angle, main_color, main_level, main_attrib, &succf, db_ptr);

if (succf == FALSE) // oops!

{

// Inform the user that the Arc creation failed

}

Link to comment
Share on other sites

hello

May I ask how to create a rev-surface?

 

I'm using code below to create rev surfaces.

but something is going wrong,

I can't select surface until I call rebuild_graphics() function.

 

Any idea?

code:

 

 

double radius=100;

 

MC_BOOL succf;

DB_LIST_ENT_PTR e_ptr;

 

surf_type surf;

memset(&surf,0,sizeof(surf_type));

surf.s.f.chksurf=0;

surf.s.type = REV_SURF;

surf.s.density=0;

 

ent e;

memset(&e,0,sizeof(ent));

e.id=A_ID;

e.u.ar.r=radius;

e.u.ar.view=2;

e.u.ar.sa=0;

e.u.ar.sw=P5_PI;

vec_3d (radius, 0.0, 0.0, e.u.ar.ep1);

vec_3d (0.0, 0.0, radius, e.u.ar.ep2);

 

store_ent (&e, &surf.u.rev.pcurve, ALIVE_BIT, main_color, main_level, main_attrib, &succf);

 

vec_3d (0.0, 0.0, 0.0, surf.u.rev.axis_pt);

vec_3d (0.0, 0.0, 1.0, surf.u.rev.axis_vect);

 

surf.u.rev.start_ang=0;

surf.u.rev.end_ang=TWO_PI;

 

create_nurbsurf_from_surf(&surf, &e);

 

e.id=SURF_ID;

store_ent (&e, &e_ptr, ALIVE_BIT, main_color, main_level, main_attrib, &succf);

 

 

 

 


Link to comment
Share on other sites

Thanks for the example code Roger. There are many things in your code that will help me. I hate to keep asking for more, but that's what you guys get for being so darn helpful biggrin.gif

 

What's the easiest way to draw an arc in the graphics window on a specified level? Thanks again.

Link to comment
Share on other sites

I saw that Roger specifies a level in his example, thank you. I was understanding that that was only in the database, but not in the graphics window? I need my users to see the circles in the window as well as select them from the database. If they are one in the same, I apologize, I'm very new at this.

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...