Class Definition: model.Viewer

drafting: model.Viewer

A window showing a solid, a triangle mesh or an assembly, in which edges, faces, solids and coordinate systems can be picked, and solids hidden or shown through.

Assigning a solid.Shape to the Model of a viewer opens its window, or redraws it in place, keeping the camera where it was; each solid is drawn in its colour, grey where it has none. Nothing else redraws it. A polymesh.Mesh is shown the same way, shaded facet by facet, however many triangles it has, in its faces’ colours where it has them, else in its vertices’, blended across each facet, else in grey. A model.Assembly is shown as its shape, each part in its colour. The show method of a shape, a mesh or an assembly keeps a viewer for each variable and is the usual way in; a viewer of your own is for when you want to hold it yourself.

model.Viewer.opacity hides solids or shows them through, to look inside a model, and pick with 'solid' tells which solid is which. See-through solids are blended so that the nearer reads as in front, whatever order they are drawn in.

The window is drawn by Open CASCADE in a process of its own, so a complex model turns smoothly and never holds up the Octave prompt. The world axes are drawn in the lower right corner, turning with the view, x red, y green and z blue. The left mouse button rotates, the middle one pans and the wheel zooms. The view turns keeping z up the window, as Octave’s rotate3d turns a plot, until T lets it turn any way. It turns about the centre of the window, wherever it has been panned to, or about a point of the shape chosen by double-clicking it; a double click that misses the shape, or fitting the view, returns to the centre of the window. During a pick a double click is a click like any other. Keys:

Ffit the shape to the window, and turn about the centre of the window again
0isometric view
1front view, looking along +y
2top view, looking down z
3right view, looking along -x
Tlet the mouse turn the view any way, or keep z up again, standing the view upright
Con a mesh, the next of the colourings it has: its faces’ colours, its vertices’, grey
Eon a mesh, its triangle edges drawn or not, not at first

What C and E choose holds for every mesh the viewer shows after, where the mesh has that colouring.

Closing the window ends the viewer; assigning a shape again opens a new one. The window closes with Octave.

The viewer needs a display to run. It runs on Linux, FreeBSD, Windows and macOS.

See also: solid.Shape.show, polymesh.Mesh.show, model.Assembly.show, model.Viewer.pick, model.Viewer.opacity

Source Code: model.Viewer

The model.Viewer class contains the following properties:

The solid.Shape, the polymesh.Mesh or the model.Assembly shown. Assigning any of them opens the window if it is not open and redraws it in place if it is. An assembly is drawn as model.Assembly.shape gives it, or as one mesh where a part of it is a mesh, and its solids are chosen by name and path in model.Viewer.opacity. The empty shape opens the window empty, or clears it if it is open; the first shape shown after it is fitted to the window.

The name of the variable holding the shape shown. Setting it titles the window with the name, so that several viewers can be told apart, and the queries model.Viewer.pick prints use it. Until it is set the window is titled drafting and the queries use 'S'.

The model.Viewer class offers the following public methods:

model.Viewer: V = model.Viewer ()

model.Viewer: V = model.Viewer ('Hidden', TF)

V = model.Viewer () returns a viewer that shows nothing yet; its window opens when a shape, even the empty one, is assigned to its Model:

 
 V = model.Viewer ();
 V.Model = solid.box (80, 40, 12);
 V.Model = hole (V.Model, [20, 20, 12], 8, Inf);

V = model.Viewer ('Hidden', true) never shows its window. It draws and picks all the same, which is how the viewer is tested.

model.Viewer: [E, F] = pick (V)

model.Viewer: [E, F, ECODE, FCODE] = pick (V)

model.Viewer: [E, CODE] = pick (V, 'edge')

model.Viewer: [F, CODE] = pick (V, 'face')

model.Viewer: [I, CODE] = pick (V, 'solid')

model.Viewer: [P, F] = pick (V)

[E, F] = pick (V) waits while you click edges and faces in the viewer’s window, then returns the indices of the edges in E and of the faces in F, in the order you picked them, in the numbering solid.Shape.edges and solid.Shape.faces use. A click selects what is under the pointer, which stays drawn in orange, a selected edge thick and a selected face filled, and a second click on it lets it go; the shape can still be turned between clicks. Enter finishes and Escape cancels, returning nothing. With 'edge' or 'face' only that kind can be picked, and its indices are the first output.

Indices belong to the shape they were picked on, and a script that holds them breaks as soon as an earlier step adds a feature. So pick also returns, as a character vector, the code that selects the same items by kind, direction and position: CODE for one kind, and ECODE and FCODE for edges and faces picked together, empty where none was picked. The code selects exactly the items picked: one query where what they share finds them alone, else one query for each, joined, and their indices only where an item cannot be told apart from the others by any query. It names the shape by the variable the viewer shows, and eval (CODE) gives the indices again: in the order picked where it names the items one by one, and in increasing order where one query names them all. Put it in the script in place of the numbers:

 
 ## Four upright edges picked on a block 80 by 40 by 12
 [E, CODE] = pick (V, 'edge');
 ## CODE holds the query for the script
 E = edges (part, 'Type', 'line', 'Direction', [0, 0, 1], ...
            'Within', [0, 0, 0, 80, 40, 12]);
 part = fillet (part, E, 3);

[I, CODE] = pick (V, 'solid') picks whole solids: a click on any face of a solid selects it, and I returns the indices of the solids picked, in the order you picked them, numbered as solid.Shape.solids and model.Assembly.solids list them, for model.Viewer.opacity. The face clicked stays drawn in orange. CODE is what selects the same solids in opacity, as code: the name they share, else the path they share, where either selects them alone, else their indices.

When the viewer shows a model.Assembly, picking only tells its solids apart, since the parts of an assembly are edited as parts, not in the assembly. pick (V) then picks solids, and 'edge' and 'face' are refused.

When the viewer shows a polymesh.Mesh, [P, F] = pick (V) waits while you click points on it, each marked where it lands, and returns them in P, an N-by-3 matrix of the points where the clicks hit the mesh, and in F an N-by-4 matrix: for each point the index of the triangle it lies on, a row of the mesh’s Faces, and the triangle’s unit normal, turned as its corners run. A click that misses the mesh adds nothing. Enter finishes and Escape cancels, returning nothing. A mesh has no edges, faces or solids in the sense of a solid, so 'edge', 'face' and 'solid' are refused.

See also: solid.Shape.edges, solid.Shape.faces, solid.Shape.show, model.Viewer.opacity

model.Viewer: U = pickucs (V)

model.Viewer: U = pickucs (V, MODE)

model.Viewer: [U, CODE] = pickucs (…)

U = pickucs (V) returns the geom.UCS you pick on the shape in the viewer’s window, in two steps, each prompted at the foot of the window. geom.UCS (V) does the same.

The axes come first. Click a flat face, which gives the plane and its outward normal, then two points: the direction from the first to the second, laid in the face, is +x. The points can be anywhere on the part, two corners along an edge for instance. With MODE 'points' no face is picked; three points give the axes instead, +x from the first to the second and the third on the +y side, as for geom.UCS.threepoint. MODE is 'face' by default.

The origin comes second. Click one point and the origin is there, laid in the plane. Click a second and the origin takes its x from the first point and its y from the second, which puts a datum corner where a fillet or chamfer leaves no vertex to click. Press Enter at once to keep the origin at the first point of the axes.

Every point snaps to what it lands on: a corner to its vertex, a circular edge to its centre, another edge to its midpoint, a face to the point clicked. Each is marked in the window. On a triangle mesh the face clicked is a triangle, which gives the plane where the mesh is flat, and a point snaps to a corner of the triangle under it or the middle of one of its sides when the click is near, or else lies where it was clicked. The shape can be turned between clicks, and Escape cancels with an error.

A pick depends on the shape it was made on, so [U, CODE] = pickucs (…) also returns CODE, the expression that makes the same UCS from coordinates, as a character vector to put in the script in place of the pick. eval (CODE) gives the UCS again, to the ten significant digits CODE carries.

 
 [U, CODE] = pickucs (V);
 CODE
 ⇒ CODE = geom.UCS ([0, 0, 1], [20, 20, 12], [21, 20, 12])
 R.UCS = geom.UCS ([0, 0, 1], [20, 20, 12], [21, 20, 12]);

See also: geom.UCS, model.Viewer.pick

model.Viewer: opacity (V, WHAT, A)

model.Viewer: A = opacity (V)

opacity (V, WHAT, A) draws the solids WHAT chooses at the opacity A, from 1, opaque, as every solid is at first, to 0, hidden. A hidden solid is neither drawn nor picked, so a click goes through it to what lies behind; a solid shown through is picked as before. The window changes in place: nothing is drawn again but the solids chosen, and the view stays where it is. This is the viewer’s own setting, for looking at a model: it is not kept by the shape, and no file carries it.

WHAT chooses by any of:

indicesthe solids of those indices, in the order solid.Shape.solids and model.Assembly.solids list them
a nameevery solid of that solid.Shape.Name, and in an assembly every solid under a placement of that name, at any depth
a patha cell row of placement names from the top of the assembly, as the path field of model.Assembly.solids gives it, choosing every solid under the placement it ends at
'all'every solid

Names and paths match exactly, case included. A name or a path that matches nothing is an error. A mesh has no solids, so it takes one opacity for the whole, through 'all' or the index 1.

 
 V = show (A);
 opacity (V, 'casing', 0.3);          # see into the casing
 opacity (V, {'rotor', 'shaft'}, 0);  # and hide the shaft
 opacity (V, 'all', 1);               # everything solid again

Assigning Model again, as show does after a change to the model, keeps what was set by a name, a path or 'all' and applies it to the new model wherever it still matches. What was set by indices is dropped, since indices change with the model.

A = opacity (V) returns the opacity of every solid as a column, in the order of their indices, or the one of a mesh. A viewer whose window was closed keeps what is set, for the next model it shows.

See also: model.Viewer.pick, solid.Shape.solids, model.Assembly.solids

model.Viewer: view (V, AZ, EL)

model.Viewer: view (V, AZ, EL, ZOOM)

model.Viewer: view (V, AZ, EL, ZOOM, TARGET)

model.Viewer: [AZ, EL, ZOOM, TARGET] = view (V)

view (V, AZ, EL) turns the view to look at the shape from azimuth AZ and elevation EL, in degrees, and fits the shape to the window. The angles are those Octave’s view takes for a plot: AZ turns anticlockwise about z, seen from above, starting from the negative y axis, and EL rises from the x-y plane, from -90 looking straight up to 90 looking straight down. The z axis points up the window; looking straight down or up, the y axis does, turned by AZ. The mouse still turns the view afterwards.

view (V, AZ, EL, ZOOM) then magnifies the shape ZOOM times about the centre of the window: 1 is the shape fitted, 2 shows it twice as large and 0.5 half as large.

view (V, AZ, EL, ZOOM, TARGET) looks at the point TARGET, a 3-element vector in millimetres, instead of the middle of the shape: the point Octave calls the camera target. It is put at the centre of the window, ZOOM magnifies about it, and the mouse turns the view about it afterwards, as after a double click. So a detail is seen close up from a script.

[AZ, EL, ZOOM, TARGET] = view (V) returns the view as it is now, however it got there: the angles it looks from, AZ from -180 to 180, its magnification against the shape fitted, and the point at the centre of the window, wherever the view was panned to. Given back to view, they restore it, so a view found with the mouse can be kept in a script. After T, the mouse can also turn the view about the line of sight, which they do not hold, so such a view is restored upright.

The keys of the window give four of these views: 0 is view (V, 45, 35.26), near enough, the isometric view the window opens with, 1 is view (V, 0, 0), 2 is view (V, 0, 90) and 3 is view (V, 90, 0).

 
 ## A shallow part looks deeper than it is from the isometric view;
 ## look from higher up
 part = solid.box (80, 40, 4);
 V = show (part);
 view (V, 30, 60);
 ## Closer, on the middle of the part
 view (V, 30, 60, 2);
 ## Four times larger, on a corner
 view (V, 30, 60, 4, [80, 40, 4]);
 ## Keep a view found with the mouse, and restore it
 [az, el, zoom, target] = view (V);
 view (V, az, el, zoom, target);

See also: solid.Shape.show

model.Viewer: IMG = capture (V)

model.Viewer: capture (V, FILE)

model.Viewer: capture (…, 'Size', [W, H])

IMG = capture (V) returns what the window shows, the shape as it is turned and lit, as an H-by-W-by-3 uint8 image of red, green and blue, the size of the window: the form imread gives and image and imwrite take. The size is in the pixels of the screen, so on a Mac’s Retina screen, with two pixels to a point, the picture is twice the window’s size in points and as sharp as the window.

capture (V, FILE) writes it to FILE instead, in the format its extension names, as imwrite writes it; PNG keeps every pixel.

capture (…, 'Size', [W, H]) draws the picture W pixels wide and H high instead, whatever the size of the window, so a picture for print can be finer than the screen. The view keeps its height: a picture of another shape than the window shows more or less at the sides. Text, lines and the axes are drawn as on a screen of one pixel to a point, so the same size gives the same picture on every system.

 
 V = show (solid.cylinder (10, 30));
 view (V, 30, 20);
 capture (V, 'cylinder.png', 'Size', [1800, 1400]);

See also: model.Viewer.view, imwrite

model.Viewer: close (V)

Assigning a shape afterwards opens a new window.

model.Viewer: TF = isopen (V)