Author Topic: a first stab at something useful  (Read 156 times)

raymw

  • Newbie
  • *
  • Posts: 28
    • View Profile
a first stab at something useful
« on: 2020-08-07, 20:49:26 »
I've only been playing around with angelcad for a few weeks, but I like the structure so far, and the ood aspects of it. Not having a background in c++, I find the syntax somewhat confusing, but I know that will change as I had a load of help from Carsten. I thought I'd show a component box, which can be 3d printed. It is designed such that sizes, ventilation, standoffs can be readily changed. Depending on the quality of the 3d print, you may need to adjust the 'shave' value, so that the lid fits nicely. I recently added rounded corners to the lid, (thinlid, in code) because often internal corners of 3d prints are not sharp. There are other alterations I may or may not make, and if anyone wants to modify or otherwise use it, feel free to do so.

 
Code: [Select]
// AngelCAD code.

//inside dimensions of box
   double y= 45;
   double x= 51;
   double z= 15;
//wall thickness   
   double wall=2.5;
// x1,y1,x3,y3 defines corners of ventilation hole area
   double x1= 15;
   double y1=15;
   double x3=36;
   double y3=30;
//hole is width of inividual ventilation hole   
   double hole =3;
//space is hole spacing   
   double space= 5;
//select if you want want ventialtion holes in box or lid  or both 
   bool lidhole = true;
   bool boxhole = false;
//soh is standoff height, sod is standoff diameter   
   double soh = 5 ;
   double sod = 6 ;
//shole is diameter of hole in centre of standoff   
   double shole = 2;
// son is number of standoffs
   int son =4;
// if you add more standoffs, adjust number 'son', above.
// if you don' twant standoffs, set son to 0, but leave values in stands
// (x,y) position of standoffs 'stands'
   array<pos2d@ > stands  = {
     {6,6},
     {45,6},
     {6,39},
     {45,39}
     };
//shave is to give clearance for lid lip
//value of shave may depend on manufacturing method.   
  double shave=0.5;
 
 
 // fixed values below........
 // no need to edit values below here
 //*******************************************
 
 // w2 is half the wall thickness
   double w2= wall/2;
   
solid@ box()   //create box
{
   //smaller cuboid for inside
   solid@ boxis = cuboid(x,y,z+wall, false);
   // add walls to create outside of box
   solid@ boxos = cuboid(x+wall+wall,y+wall+wall,z+wall,false);
   //subtract inner from outer
   solid@ crate= translate(-wall,-wall,-wall)*boxos -boxis;
   //add lip to top size (0.5*wall) on outer edge
   solid@ lip= cuboid(x+wall+wall,y+wall+wall,wall) -translate(w2,w2,0)*cuboid(x+wall,y+wall,wall);
  solid@ all= translate (-wall,-wall,z-w2)*lip +crate;
return all;
}

solid@ thinlid(){
   
  //puts a 1mm radius on lip of inserted top
shape2d@ top= offset2d (rectangle (x+wall-shave-2,y+wall-shave-2),r:1);
return transform_extrude (top,translate(0,0,w2)*top);
   }

solid@ lid()  //similar to box, but only half wall high rim
{
  solid@ topbig = cuboid(x+wall+wall,y+wall+wall,w2, false);
   //add insertedhalf wall (make it shave smaller to fit inside lip of box)
   
   solid@ topall =topbig + translate(w2+1+(shave/2),w2+1+(shave/2),w2)*thinlid();
   // add lid holes (diam 1.3 * shole) in corners for fixing screws
   double r = (1.3*shole)/2;
   double hr =shole/2;
   solid@ h1 = translate (hr+wall,hr+wall,-wall)*cylinder(20,r);
   solid@ h2 = translate (hr+wall,y+wall-hr,-wall)*cylinder(20,r);
   solid@ h3 = translate (x+wall-hr,hr+wall,-wall)*cylinder(20,r);
   solid@ h4 = translate (x-hr+wall,y-hr+wall,-wall)*cylinder(20,r);
return translate (-wall,-wall,-wall)*(topall - h1 - h2 - h3 - h4);
 
 
    }
   
solid@ sup()    //support for pcb and screw holes for lid (standoff)
{
   solid@ block= cylinder(soh,sod/2,true)-cylinder(soh,shole/2,true);
   return translate(0,0,soh/2)*block;
}
 
solid@ standoffs()    // add standoffs
{
   array<solid@> p;
   for (int j=0;j<son;j++)
      {
      p.push_back( translate(stands[j].x(),stands[j].y(),0)*sup());
      }
return union3d(p);
}
 
solid@ holes()    // number of ventilation holes in each direction
{
   int nx= int((x3-x1)/space) ;
   int ny= int((y3-y1)/space) ;
   double dx=space;
   double dy=space;
   // create an array of cuboids a grid pattern in 1st quadrant
   array<solid@> h;
   for (int ix=0;ix<=nx;ix++)
   {
      for (int iy=0;iy<=ny;iy++)
      {
         double xh = x1 + ix*space;
         double yh = y1 + iy*space;
       h.push_back(translate(xh,yh,0)*cuboid(hole,hole,100,true));
      }
   }
   // return the cylinders as a single solid
return union3d(h);   
}

solid@ fix()     // fix standoffs at top corners for screws for lid
{
   double hr =shole/2;
   solid@ h1 = translate (hr,hr,z-soh)*sup();
   solid@ h2 = translate (hr,y-hr,z-soh)*sup();
   solid@ h3 = translate (x-hr,hr,z-soh)*sup();
   solid@ h4 = translate (x-hr,y-hr,z-soh)* sup();
return h1 + h2 + h3 + h4;
}
 
solid@ boxholed()  //add ventilation holes to box if required
{
   if (boxhole == true)
      {     
      return box() - holes();
      }
return box();
}
           
solid@ lidholed()  //add ventilation holes to lid if required
{
   if (lidhole == true)
     {
      return lid() -holes();
      }
return lid();     
}
     
void main()    //put all the parts together
{
   solid@ both=boxholed()+translate(0,y+3+wall+wall,0)*lidholed() +fix()+standoffs();
   both.write_xcsg(GetInputFullPath(),secant_tolerance:-1.0);
}

edited the above code to correctly offset resized rounded corner lid part - hopefully fits now!
« Last Edit: 2020-08-29, 22:07:38 by raymw »

Carsten Arnholm

  • Administrator
  • Newbie
  • *****
  • Posts: 44
    • View Profile
Re: a first stab at something useful
« Reply #1 on: 2020-08-10, 19:06:31 »
Hello and welcome to the forum!

Very nicely made box! I have attached an image made from it. One thing to note are these warnings you get at the end of the compilation:

Orphaned object = 0x5574545bbac0
Orphaned object = 0x557454615f20
Orphaned object = 0x55745461d570
Orphaned object = 0x55745461d950


The reason for this is that you have declared an array<pos2d2> in the global scope. That means the array lives beyond the end of main(), and therefore you get a warning for each object in the array when main() returns. This does no harm though, it is merely information. You can, however eliminate the warnings yourself by erasing the array at the end of main(), like this:

void main()    //put all the parts together
{
   solid@ both=boxholed()+translate(0,y+3+wall+wall,0)*lidholed() +fix()+standoffs();
   both.write_xcsg(GetInputFullPath(),secant_tolerance:-1.0);
   stands.resize(0);  // <-- clear the array contents by resizing it to zero length
}

« Last Edit: 2020-08-11, 16:43:54 by Carsten Arnholm »

raymw

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: a first stab at something useful
« Reply #2 on: 2020-08-11, 18:43:22 »
Thanks, I never actually saw the warning messages. I test printed the boxes as I wrote the code,  I have about 18 pla boxes, not many any use (trying to sort the printer out at the same time). I've now added rounded edges, and tried to do rounded corners, too, but not much success there. Easier to use a bit of sandpaper! I don't think I'll 'counterbore' the screwholes.

Carsten Arnholm

  • Administrator
  • Newbie
  • *****
  • Posts: 44
    • View Profile
Re: a first stab at something useful
« Reply #3 on: 2020-08-11, 19:01:40 »
Assuming you have not added the stands.resize(0); statement as suggested above, press F5 to see the warning messages.

F5 is the same as Build -> Compile AngelScript
F6 does the same as F5, but also runs XCSG to build the final STL. Then you can miss such warnings unless they are serious to cause a stop in compilation.

raymw

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: a first stab at something useful
« Reply #4 on: 2020-08-11, 21:56:30 »
I didn't see the warning messages, since they scrolled up through the narrow window at the bottom of the editor. I noticed the various 'red text', marking errors in my script, when they occurred,  but as the final lines said everything was OK, i never bothered to scroll back through. I've set the array to zero, as you suggested, and now no warnings. Is it possible to import dxf files?

Carsten Arnholm

  • Administrator
  • Newbie
  • *****
  • Posts: 44
    • View Profile
Re: a first stab at something useful
« Reply #5 on: 2020-08-12, 06:15:25 »
The console window height is resizeable. Grab the edge between it and the editor and resize it.

importing dxf is a 2-step process-

1. outside AngelCAD, run 'dxfread' in a terminal window to generate an AngelCAD function from it
$ dxfread -asfunc myfile.dxf
this generates a file myfile.as containing the profile from DXF.

Note: You must set up the PATH in windows or linux so the system, finds dxfread.

2. in AngelCAD, include the generated file

#include "myfile.as"

Then, somewhere in your code, call

shape2d@ profile = myfile();

obviously you can linear_extrude it.

WillAdams

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: a first stab at something useful
« Reply #6 on: 2020-08-25, 04:59:52 »
Got a bunch of error messages which I'd written up in a post which was discarded 'cause .as isn't an allowed file extension for attachments.

See the attached .txt file.

Carsten Arnholm

  • Administrator
  • Newbie
  • *****
  • Posts: 44
    • View Profile
Re: a first stab at something useful
« Reply #7 on: 2020-08-25, 10:00:42 »
Got a bunch of error messages which I'd written up in a post which was discarded 'cause .as isn't an allowed file extension for attachments.

See the attached .txt file.

This file was corrupted because of some strange characters, the errors have nothing to do with AngelCAD coding as such. Be careful with using special character encodings. When posting code to this forum, please use the # button above to insert code so we avoid this kind of issue. I have attached the code that you submitted, it runs fine after removing non-standard (and invisible) special characters.

Code: [Select]
// AngelCAD code.

//inside dimensions of box
double y= 45;
double x= 51;
double z= 15;
//wall thickness
double wall=2.5;
// x1,y1,x3,y3 defines corners of ventilation hole area
double x1= 15;
double y1=15;
double x3=36;
double y3=30;
//hole is width of inividual ventilation hole
double hole =3;
//space is hole spacing
double space= 5;
//select if you want want ventialtion holes in box or lid or both
bool lidhole = true;
bool boxhole = false;
//soh is standoff height, sod is standoff diameter
double soh = 5 ;
double sod = 6 ;
//shole is diameter of hole in centre of standoff
double shole = 2;
// son is number of standoffs
int son =4;
// if you add more standoffs, adjust number 'son', above.
// if you don' twant standoffs, set son to 0, but leave values in stands
// (x,y) position of standoffs 'stands'

array<pos2d@> stands = {
{6,6},
{45,6},
{6,39},
{45,39}
};
//shave is to give clearance for lid lip
//value of shave may depend on manufacturing method.
double shave=0.5;


// fixed values below........
// no need to edit values below here
//*******************************************

// w2 is half the wall thickness
double w2= wall/2;

solid@ box()  //create box
{
//smaller cuboid for inside
solid@ boxis = cuboid(x,y,z+wall, false);
// add walls to create outside of box
solid@ boxos = cuboid(x+wall+wall,y+wall+wall,z+wall,false);
//subtract inner from outer
solid@ crate= translate(-wall,-wall,-wall)*boxos -boxis;
//add lip to top size (0.5*wall) on outer edge
solid@ lip= cuboid(x+wall+wall,y+wall+wall,wall) -translate(w2,w2,0)*cuboid(x+wall,y+wall,wall);
solid@ all= translate (-wall,-wall,z-w2)*lip +crate;
return all;
}

solid@ thinlid(){
//puts a 1mm radius on lip of inserted top
shape2d@ top= offset2d (rectangle (x+wall-shave-2,y+wall-shave-2),r:1);
return transform_extrude (top,translate(0,0,w2)*top);
}

solid@ lid()  //similar to box, but only half wall high rim
{
solid@ topbig = cuboid(x+wall+wall,y+wall+wall,w2, false);
//add insertedhalf wall (make it shave smaller to fit inside lip of box)
solid@ topall =topbig + translate(w2+1+(shave/2),w2+1+(shave/2),w2)*thinlid();
// add lid holes (diam 1.3 * shole) in corners for fixing screws
double r = (1.3*shole)/2;
double hr =shole/2;
solid@ h1 = translate (hr+wall,hr+wall,-wall)*cylinder(20,r);
solid@ h2 = translate (hr+wall,y+wall-hr,-wall)*cylinder(20,r);
solid@ h3 = translate (x+wall-hr,hr+wall,-wall)*cylinder(20,r);
solid@ h4 = translate (x-hr+wall,y-hr+wall,-wall)*cylinder(20,r);
return translate (-wall,-wall,-wall)*(topall - h1 - h2 - h3 - h4);
}

solid@ sup() //support for pcb and screw holes for lid (standoff)
{
solid@ block= cylinder(soh,sod/2,true)-cylinder(soh,shole/2,true);
return translate(0,0,soh/2)*block;
}

solid@ standoffs()// add standoffs
{
array<solid@> p;
for (int j=0;j<son;j++)
{
p.push_back( translate(stands[j].x(),stands[j].y(),0)*sup());
}
return union3d(p);
}

solid@ holes()// number of ventilation holes in each direction
{
int nx= int((x3-x1)/space) ;
int ny= int((y3-y1)/space) ;
double dx=space;
double dy=space;
// create an array of cuboids a grid pattern in 1st quadrant
array<solid@> h;
for (int ix=0;ix<=nx;ix++)
{
for (int iy=0;iy<=ny;iy++)
{
double xh = x1 + ix*space;
double yh = y1 + iy*space;
h.push_back(translate(xh,yh,0)*cuboid(hole,hole,100,true));
}
}
// return the cylinders as a single solid
return union3d(h);
}

solid@ fix()
{
   double hr =shole/2;
   solid@ h1 = translate (hr,hr,z-soh)*sup();
   solid@ h2 = translate (hr,y-hr,z-soh)*sup();
   solid@ h3 = translate (x-hr,hr,z-soh)*sup();
   solid@ h4 = translate (x-hr,y-hr,z-soh)* sup();
   return h1 + h2 + h3 + h4;
}

solid@ boxholed() //add ventilation holes to box if required
{
   if (boxhole == true) {
      return box() - holes();
   }
   return box();
}

solid@ lidholed() //add ventilation holes to lid if required
{
   if (lidhole == true)
   {
    return lid() -holes();
   }
   return lid();
}

void main()//put all the parts together
{
   solid@ both=boxholed()+translate(0,y+3+wall+wall,0)*lidholed() +fix()+standoffs();
   both.write_xcsg(GetInputFullPath(),secant_tolerance:-1.0);
   
   stands.resize(0);
}



raymw

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: a first stab at something useful
« Reply #8 on: 2020-08-25, 16:24:18 »
I've added rounded edge choice, up to 16 stand-offs, choice of lid fixings or not, and written a gui for it. Works fine for me, can 3d print any component box I may need, but buying from Amazon is almost as quick! I've not bothered with any data error checking, errors are pretty apparent because the 3d view appears pretty quick, and shows quite good detail.