GO2cam Javascript API  6.11
Create and Edit Geometry

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

  1. 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)
  2. 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)
  3. 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))