Difference operation from root - openscad

Is there a way to make difference directly from root? I want to do something like this:
difference(){
root();
cube();
}
instead of this:
difference(){
union(){
object1();
object2();
.
.
objectN();
}
cube();
}

You are already on the there. You just need to define root() and cube() as modules like so:
difference(){
root();
cube();
}
module root(){
object1();
object2();
...
}
module cube(){
//some cube definition
}
In the background OpenSCAD will secretly create a union() around every module for you, just as if you wrote:
module xyz(){
union(){
//objects
}
}
So in this example an object root() is created and cube() will be subtracted from this object. By using the module definition you keep your code just as modular as you ask for.
You may also want to have a look at the official docu and cheat sheet here.

difference() module subtracts all other children from the first one. In first snippet, first child is root(), in second first child is union(), and in both cube is subtracted.

Related

openscad drill into group created by for

I know that when I use for it creates a group of the generated children.
I created a module called grid like so:
module grid(x0,y0,dx,dy,nx,ny) {
for (x=[0:1:nx-1]) {
for(y=[0:1:ny-1]) {
i=x*nx+y;
echo(i);
translate([x0+x*dx,y0+y*dy,0]) children(i);
}
}
}
which when used like this:
grid(-50,-50,25,25,5,5) {
cube([10,10,10],center=true);
cube([10,10,10],center=true);
cube([10,10,10],center=true);
cube([10,10,10],center=true);
//.. continue to create 25 cubes total
}
arranges the cubes in a nice grid.
however my original hope and intention was to use it like this:
grid(-50,-50,25,25,5,5) {
for(i=[0:1:24]) {
cube([10,10,10],center=true);
}
}
Which fails because the for operator returns a group and not a set of children.
Why does the for add a group to begin with? (also leading to the need for intersection_for)
And is there a way for my Grid operator module to handle the children of the group?
I personally hope for the grouping/union for elements within a for() to become optional at some time.
If you don't mind compiling OpenSCAD from source, you could try it already today.
There is an ongoing issue Lazy union (aka. no implicit union)
and a patch here Make for() UNION optional
Just updated my knowledge of OpenSCAD, there is a better solution:
module nice_cube()
{
translate([0,0,$height/2]) cube([9,9,$height], center = true);
}
module nice_cylinder()
{
translate([0,0,$height/2]) cylinder(d=10,h=$height, center = true);
}
module nice_text()
{
linear_extrude(height=$height, center=false) text(str($height), size=5);
}
module nice_grid()
{
for(i=[0:9], j=[0:9])
{
$height=(i+1)*(j+1);
x=10*i;
y=10*j;
translate([x,y,0]) children();
/* let($height=(i+1)*(j+1)) {children();} */
}
}
nice_grid() nice_cube();
translate([0,-110,0]) nice_grid() nice_text();
translate([-110,0,0]) nice_grid() nice_cylinder();
The trick here is to control the shape produced by module by special variables (starting with $) those can be used like in example, commented line using let() requires development version of openscad.
I guess You want this:
for(x=[...], y=[...]) {
translate([x,y,0]) children();
}
Note, that you need only one for statement to loop through both x and y values.
What I have understood from your comment is, that you want your objects in the nodes of the grids to be parametric, and parameter depends on index. This requirement was not mentioned in the original question. In this case, solution depends on your problem context, I guess. The two possibilities I see are:
module grid_of_parametric_modules(other_param)
{
for(i=[0:24])
{
x=_x(i);
y=_y(i);
translate([x,y,0]) parametric_module(i_param(i), other_param);
}
}
However this may not be suitable, especially if You are going to add new shapes to your grid in future. Then you can probably do it like:
function grid_pos(i) = [_x(i), _y(i), 0];
....
for(i=[0:24])
translate(grid_pos(i)) parametric_module(i);

comprehensive list of graphics object types matlab

I could not find a comprehensive graphics object types list that could be especially useful to find an modify objects in matlab plots such as:
findall(gcf,'Type','Line'); %get all line objects in the current figure
To be more precise: I'm looking for a list of possible input after the argument 'Type'.
I am not aware of a way to get the comprehensive list, but you can get the list of types for existing objects:
types = unique(get(findall(gcf, '-property', 'Type'), 'Type'));
Practically I think this is enough, since we are not interested in those non-existing objects.
You can access to all graphics objetcs through the packages embedding the type of one plot. For this, use metaclass to go through metadata of a plot type:
h = plot(rand(10,1));
% h is of type Line
Now we use metaclass to get all metadata about class Image:
metah = metaclass(h);
One nice feature is that it references the ContainingPackage, in which we can acces to all classes through ClassList. In each class you can get the name, classs metadata, and ways to navigate through class hierarchy.
For our purpose, I simmply get the complete list of primitive graphics with
{metah.ContainingPackage.ClassList.Name}
It gives the following list (after sorting) on R2017b:
{'matlab.graphics.chart.primitive.Area' }
{'matlab.graphics.chart.primitive.Bar' }
{'matlab.graphics.chart.primitive.Binscatter' }
{'matlab.graphics.chart.primitive.ConstantLine' }
{'matlab.graphics.chart.primitive.Contour' }
{'matlab.graphics.chart.primitive.Data3D' }
{'matlab.graphics.chart.primitive.ErrorBar' }
{'matlab.graphics.chart.primitive.FunctionLine' }
{'matlab.graphics.chart.primitive.GraphPlot' }
{'matlab.graphics.chart.primitive.Heatmap' }
{'matlab.graphics.chart.primitive.Histogram' }
{'matlab.graphics.chart.primitive.Histogram2' }
{'matlab.graphics.chart.primitive.Line' }
{'matlab.graphics.chart.primitive.Quiver' }
{'matlab.graphics.chart.primitive.Scatter' }
{'matlab.graphics.chart.primitive.Stair' }
{'matlab.graphics.chart.primitive.Stem' }
{'matlab.graphics.chart.primitive.Surface' }
{'matlab.graphics.chart.primitive.brushingUtils'}
Last one is not a graphics primitive. You can see that it lacks classes like Image, which can be accessed through the same process:
h = image(rand(5));
metah = metaclass(h);
sort({metah.ContainingPackage.ClassList.Name}.'}
which gives:
{'matlab.graphics.primitive.Data' }
{'matlab.graphics.primitive.Group' }
{'matlab.graphics.primitive.Image' }
{'matlab.graphics.primitive.Light' }
{'matlab.graphics.primitive.Line' }
{'matlab.graphics.primitive.Marker' }
{'matlab.graphics.primitive.Patch' }
{'matlab.graphics.primitive.Polygon' }
{'matlab.graphics.primitive.Rectangle' }
{'matlab.graphics.primitive.Surface' }
{'matlab.graphics.primitive.SurfaceXYZData'}
{'matlab.graphics.primitive.Text' }
{'matlab.graphics.primitive.Transform' }
Some classes seem to be in both packages. I think the second list is the core classes and the first list some dedicated classes for line charts (not patched). But this is just a guess.
I think we should have all basic graphics classes, or that there are other graphics that are in other packages -- if anyone knows what are these other packages, let us know!
EDIT
Finally I've found the page of the documentation listing all graphic objects. See here

Extending a class in another file

I have some TypeScript code that is being generated by a tool. I'd like to extend this class in another file. As of 0.9.1.1, what's the best way to go about this?
I thought maybe I could staple my additional functions onto the prototype, but this is giving various errors (which change depending what mood the compiler is in).
For example:
Foo.ts (generated by a tool)
module MyModule {
export class Dog { }
}
Bar.ts
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
You cannot split a class definition between multiple files in TypeScript. However typescript understands how JavaScript works and will let you write idomatic JavaScript classes just fine:
module MyModule {
export function Dog(){};
}
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
Try it online
One way around this is to use inheritance:
class BigDog extends Dog{
bark(){}
}
I have encountered your problem as well before, but I had some deeper problems. You can see from basarat's example, that simple functions can be added as an extension to the prototype, but when it comes to static functions, or other static values you might want to extend your (presumably third party) class, then the TSC will warn you, that there is no such method defined on the class statically.
My workaround was the following little hack:
module MyModule {
export function Dog(){};
}
// in the other file
if (typeof MyModule !== 'undefined'){
Cast<any>(MyModule.Dog).Create = ()=>{return new Dog();};
}
// where Cast is a hack, for TS to forcefully cast types :)
Cast<T>(element:any):T{ return element; }
This should cast MyModule.Dog, to an any object, therefore allowing attachment of any kinds of properties, functions.

Exposing boost::gregorian::date to Python using Boost.Python

I'd like to make boost::gregorian::date available to Python using Boost.Python. But how do I create a decent __str__ function when one is not available on the Boost date class? I'd like to write it like this:
BOOST_PYTHON_MODULE(mymodule)
{
class_<boost::gregorian::date>("Date")
.add_property("year", &boost::gregorian::date::year)
.add_property("month", &boost::gregorian::date::month)
.def("__str__", ???)
;
}
After some research I found the answer. You can supply static functions to .def as well. Just give it to_iso_extended_string and it gets the object as first argument.
BOOST_PYTHON_MODULE(mymodule)
{
class_<boost::gregorian::date>("Date")
.add_property("year", &boost::gregorian::date::year)
.add_property("month", &boost::gregorian::date::month)
.def("__str__", &to_iso_extended_string)
;
}

In Tritium, is there a way to assign an id based on child number?

I have a table with a series of rows. I want to change them into divs, but maintain (somehow) their positional information. At the moment, this is what I'm doing:
$("./tr[1]") {
add_class("mw_old_row_1")
}
$("./tr[2]") {
add_class("mw_old_row_2")
}
$("./tr") {
name("div")
}
But this isn't ideal because:
It's super-repetitive
I don't know how many rows there are
Is there a way to take the child number and include that in the class I'm assigning?
Yup, you want to make use of the index() function. Below is the example you wrote reworked using index():
$("./tr") {
add_class("mw_old_row_" + index())
name("div")
}
Below is a link with the following example in tritium tester: http://tester.tritium.io/775895b154e8e2ce99e100967299c10d73dbeb91