Create Geometry
Javascript is an object oriented language. The API is made of classes allowing to manage geometric entities. All GO2cam classes begin with the prefix GO2S and each entity has a corresponding object. For exemple, segments are managed by the object GO2SSegment, solids by GO2SSolid...etc.
GO2SXYZ object
GO2SXYZ object is the entry point for creating geometry. It is basically a vector used to store (x,y,z) coordinates (vector or position). But in some cases this object can be link to entities and store more information than simple coordinates
For exemple the code bellow draw a segment and use is as a circle diameter
var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(50,20,0)
var segment = new GO2SSegment(p1,p2)
// compute the center
var center = p1.Added(p2)
center.Scale(0.5)
var circle = GO2SCircleCreate.From2Points(center,p2)
But in this case segment and circle are totally independant as they are both built using (x,y,z) coordinates only. After drawing in this way, changing the segment coordinates will not affect the circle.
The other code below allows to create linked entities with construction history. Here center and end are GO2SXYZ objects storing the link to the segment and not only XYZ coordinates
var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(50,20,0)
var segment = new GO2SSegment(p1,p2)
var center = segment.GetMidPoint()
var end = segment.GetLastPoint()
var circle = GO2SCircleCreate.From2Points(center,end)
GO2SXYZ are just construction or computation objects. They do not exist in the database. To create points visible on screen and stored in the database, use GO2SPoint.
Construct geometry
Most of geometry entities can be created by several ways. For example, all of the 3 codes bellow do the same
- Default constructor and methods
var c = new GO2SXYZ
var p1 = new GO2SXYZ(10,0,0)
var p2 = new GO2SXYZ(0,10,0)
var arc = new GO2SArc
arc.SetFromCenterStartEnd(c, p1, p2)
- Helper class GO2SxxxCreate classes
var c = new GO2SXYZ
var p1 = new GO2SXYZ(10,0,0)
var p2 = new GO2SXYZ(0,10,0)
var arc = GO2SArcCreate.FromCenterStartEnd(c, p1, p2)
- Direct constructor. For example, GO2SArc constructor from 3 points builds an arc considering center, start and end points
var c = new GO2SXYZ
var p1 = new GO2SXYZ(10,0,0)
var p2 = new GO2SXYZ(0,10,0)
var arc = new GO2SArc(c, p1, p2)
Edit Geometry
Objects are identified by their label, set in GO2cam using contextual menu Right click > Label
The label can be read or set by script using GO2SGeometry.GetName and GO2SGeometry.SetName
Getting entities
The class GO2SGeometryUtil provides functions to find entities by type, color, layer, or by name. For example
//get all red circles in a tab
var redCircles = GO2SGeometryUtil.GetCircle(0x0000FF)
//get the geometry named 'MyCircle'
var circle = GO2SGeometryUtil.GetByName('MyCircle')[0] // Get the first found
Using GO2SInteract it is also possible to interact with the 3D scene while the script is running and select objects
Example 1:
//Draw a nurbs from selected points
//---------------------------------
//Start the selection and wait the user to confirm it
GO2SInteract.StartSelection()
//Get the selection
var geom = GO2SGeometryUtil.GetSelected()
//Use the selection
var pts = []
geom.forEach( g => { if(g.IsPoint()) pts.push(g.toXYZ()) } )
var nurbs = new GO2SNurbs(pts, true, -1)
Example 2:
//Draw a line paralell to a selected line and crossing an selected point
//----------------------------------------------------------------------
var line
while(line==undefined)
{
// launch entity picking and wait for it
line = GO2SInteract.PickEntity()
// check the click is done on a segment or a line
if(!line.IsLinear())
line = undefined
}
//launch xyz picking and wait for it
var pt = GO2SInteract.PickXYZ()
GO2SLineCreate.ParallelByPoint(pt, line)
Edit entities
Each geometry entity can have one of several parameters modified (radius, length...) The parameter list common to all entity is available through GO2SEnum.GO2SParam For each geometry entity (GO2SGeometry), all it is possible to know if a parameter can be modified or not
A parameter is editable if the geometry is compatible. Radius is not editable for a segment, but length is.
A parameter is editable if the value is not constraint by other geometry (it is not possible to change the radius of a circle if it is built by crossing a point)
For example the code bellow:
var center = new GO2SXYZ
var point = new GO2SPoint(10,10,0)
//Create a circle crossing a point -> radius is locked by point position
var circle1 = GO2SCircleCreate.From2Points(center,point.toXYZ())
var editable = circle1.IsParamEditable(GO2SParam.radius)
console.log("radius editable on circle 1: " +editable + '\n')
//Create a circle with a radius -> can be changed
var circle2 = GO2SCircleCreate.FromRadius(center,12)
editable = circle2.IsParamEditable(GO2SParam.radius)
console.log("radius editable on circle 2: " +editable)
will give the following output:
radius editable on circle 1: false
radius editable on circle 2: true
Multiple geometry
Entities made of several wire entities, such as rectangle shape or profile inherit from GO2SMultipleGeometry. Here is an example of how to deal with such a geometry:
//get the existing profile
var prf = GO2SGeometryUtil.GetByName('profile')[0] // Get the first found
console.log('the profile is made of ' + prf.GetElementCount() + ' elements\n')
//define a z ranslation
var matrix = new GO2SMatrix
matrix.DefineTranslation(new GO2SXYZ(0,0,30))
//explode the profile to access each elements individually
var elts = prf.Explode()
//translate each element
for(let i = 0; i<elts.length; i++)
{
var geom = elts[i].Transformed(matrix)
geom.SetColor(0x0000FF) // set the translation red
elts[i].Delete() // delete the exploded element that is overlaping the profile and is not needed
}
Output:
the profile is made of 4 elements
The previous example can also be written in this way, by moving the profile instead of moving exploded elements
//get the existing profile
var prf = GO2SGeometryUtil.GetByName('profile')[0] // Get the first found
//define a z ranslation
var matrix = new GO2SMatrix
matrix.DefineTranslation(new GO2SXYZ(0,0,30))
//move the whole profile
var prf2 = prf.Transformed(matrix)
//explode the profile to access each elements individually
var elts = prf2.Explode()
prf2.Delete(); // delete the moved profile that is no more needed
//change color of each element
elts.forEach( e => e.SetColor(0x0000FF))
Temporary geometry and workspaces
Workspaces
Up to now, we saw how to create and edit geometric entities using GO2SGeometry children classes. Each of these objects is automatically created and visible in the 3D scene as if you created it manually with GO2cam design menu. But it is not always what we want. Sometimes it is useful to create temporary or intermediate objects for computation purposes, without having them in the 3D scene. In this case, GO2SGeometry objects must be created in a separate workspace with GO2SWorkspace.
// compute in a workspace: its geometries are never displayed nor kept
let ws = new GO2SWorkspace()
ws.Activate()
let org = new GO2SXYZ(0, 0, 0)
let line = GO2SLineCreate.FromAngle(org, 45)
let circle = GO2SCircleCreate.FromRadius(org, 10)
// preview of the workspace geometries
let lineDisp = new GO2SDisplayGeometry(line)
lineDisp.SetColor(0x808080)
lineDisp.SetLineType(GO2SLineType.Dotted)
let circleDisp = new GO2SDisplayGeometry(circle)
circleDisp.SetColor(0x0000FF)
circleDisp.SetLineWidth(3)
// each pick moves the center of the circle, ESC validates the last one
let pos
while ((pos = GO2SInteract.PickXYZ('Pick the center of the circle, ESC to validate')))
{
// keep only the coordinates: the picked point may refer to an entity of Main
let center = new GO2SXYZ(pos.X(), pos.Y(), pos.Z())
circle = GO2SCircleCreate.FromRadius(center, 10)
circleDisp.SetGeometry(circle)
}
// intersection points computed in the workspace...
let points = new GO2SIntersect(line, circle).GetIntersections().map(i => new GO2SPoint(i))
// ...then only the result is copied into Main
let main = GO2SWorkspaceUtil.GetMain()
main.Activate()
main.Copy(circle)
points.forEach(p => main.Copy(p))
Preview under the mouse
It is possible to show a temporary "preview" geometry while the user is picking a point. Use the static method PickXYZWithPreview of GO2SInteract. The preview follows the mouse: its origin (0, 0, 0) is placed under the cursor, so draw it around the origin. The preview geometries are deleted once the point is picked.
// Define the geometry that will be shown as a 'target' preview, around the origin
// 2 segments (a simple "+" shape)
let start1 = new GO2SXYZ(-10, 0, 0); // first segment start
let end1 = new GO2SXYZ( 10, 0, 0); // first segment end
let start2 = new GO2SXYZ(0, 10, 0); // second segment start
let end2 = new GO2SXYZ(0, -10, 0); // second segment end
let line1 = new GO2SSegment(start1, end1);
let line2 = new GO2SSegment(start2, end2);
// a circle centred on the origin
let circleCenter = new GO2SXYZ(0, 0, 0);
let circle = new GO2SCircle(circleCenter, 8);
// Show the preview while the user picks a point
let pickedPoint = GO2SInteract.PickXYZWithPreview(
[line1, line2, circle], // array of preview geometries
"Click the position", // optional status-bar message
true // show coordinates next to the cursor
);
// Use the picked point to create the real geometry (null if ESC was pressed)
if (pickedPoint)
new GO2SCircle(pickedPoint, 10);