GO2cam Javascript API
List of examples

Wireframe design

Arc

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)
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)
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)

Chamfer

// create 3 points
var A = new GO2SXYZ(-10,-10,0)
var B = new GO2SXYZ(10,-10,0)
var D = new GO2SXYZ(-10,10,0)
// create 2 segments between points
var AB = new GO2SSegment(A,B)
var DA = new GO2SSegment(D,A)
// create a 5mm chamfer between segments
new GO2SChamfer(DA,AB,5)

Fillet

// Two meshing spur gears on their shafts, fully parametric:
// change the module or the number of teeth and run the script again
const module = 2 // size of the teeth (mm)
// point (x, y, z) of the frame of 'center', turned by 'angle' (rad) around Z
function Local(center, x, y, z, angle)
{
return new GO2SXYZ(
center.X() + x * Math.cos(angle) - y * Math.sin(angle),
center.Y() + x * Math.sin(angle) + y * Math.cos(angle),
center.Z() + z)
}
// spur gear: body + teeth + hub, minus bore, keyway and lightening holes
function Gear(center, nbTeeth, width, bore, nbHoles, phase, color)
{
const pitch = module * nbTeeth / 2 // pitch radius
const root = pitch - 1.25 * module // root radius
const tip = pitch + module // tip radius
const half = Math.PI * module / 5 // half thickness of a straight tooth
const gear = GO2SSolidCreate.Cylinder(center, root, width)
// one tooth every 360/nbTeeth degrees
const teeth = []
for (let i = 0; i < nbTeeth; i++)
{
const a = phase + 2 * Math.PI * i / nbTeeth
teeth.push(GO2SSolidCreate.Cuboid(
Local(center, root - 1, -half, 0, a),
Local(center, tip, -half, 0, a),
Local(center, tip, half, 0, a),
width))
}
// hub
const hubOrg = Local(center, 0, 0, -5, 0)
teeth.push(GO2SSolidCreate.Cylinder(hubOrg, 2 * bore, width + 10))
gear.MultiUnion(teeth)
// bore with its keyway
const cutOrg = Local(center, 0, 0, -10, 0)
const cuts = [GO2SSolidCreate.Cylinder(cutOrg, bore, width + 20)]
cuts.push(GO2SSolidCreate.Cuboid(
Local(center, 0, -bore / 4, -10, 0),
Local(center, bore + 2, -bore / 4, -10, 0),
Local(center, bore + 2, bore / 4, -10, 0),
width + 20))
// lightening holes between the hub and the teeth
const holeDist = (root + 2 * bore) / 2
const holeRadius = (root - 2 * bore) / 4
for (let i = 0; i < nbHoles; i++)
{
const a = 2 * Math.PI * i / nbHoles
const holeOrg = Local(center, holeDist, 0, -1, a)
cuts.push(GO2SSolidCreate.Cylinder(holeOrg, holeRadius, width + 2))
}
gear.MultiSubtract(cuts)
gear.SetColor(color)
gear.SetName(`Gear Z${nbTeeth}`)
return gear
}
// a big gear and a small one, placed at the right distance to mesh
const z1 = 40, z2 = 20
const c1 = new GO2SXYZ(0, 0, 0)
const c2 = new GO2SXYZ(module * (z1 + z2) / 2, 0, 0)
Gear(c1, z1, 15, 8, 6, 0, 0xC08040)
// the small one is turned by half a tooth to fit between the teeth
Gear(c2, z2, 15, 5, 0, Math.PI + Math.PI / z2, 0x2080E0)
// shafts
for (const [center, radius] of [[c1, 8], [c2, 5]])
{
const shaft = Local(center, 0, 0, -30, 0)
GO2SSolidCreate.Cylinder(shaft, radius, 75).SetColor(0x808080)
}


Nurbs

var pts = []
pts.push(new GO2SXYZ(0, 0, 0))
pts.push(new GO2SXYZ(12, 5, 0))
pts.push(new GO2SXYZ(15, 10, 0))
pts.push(new GO2SXYZ(20, 20, 0))
var nurbs = new GO2SNurbs(pts, false, -1)
var s1 = new GO2SSegment(new GO2SXYZ(-100,15,0),new GO2SXYZ(-50,30,0))
var s2 = new GO2SSegment(new GO2SXYZ(45,15,0),new GO2SXYZ(120,-10,0))
var start = s1.GetLastPoint()
var end = s2.GetFirstPoint()
var p1 = new GO2SXYZ(-10, -10, 0)
var p2 = new GO2SXYZ(0, 0, 0)
var p3 = new GO2SXYZ(10, -10, 0)
var nurbs = GO2SNurbsCreate.FromPointsWTangency([start,p1,p2,p3,end], true, true)
var elts = nurbs.ToArcAndSegment(0.01, 9999)
nurbs.Delete()
console.log("nurbs converted in " + elts.length +" elements")

Oblong

var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(10,100,0)
var obl1 = new GO2SOblong(p1,p2,20)
var p0 = new GO2SXYZ(50,50,0)
var obl2 = GO2SOblongCreate.FromCenter(p0, 30, 90, 20)

Ellipse

var center = new GO2SXYZ(50,50,0)
var pMajor = new GO2SXYZ(120,60,0)
var pMinor = new GO2SXYZ(40, 40, 0)
var ellipse = new GO2SEllipse(center, pMajor, pMinor)
center = new GO2SXYZ(-50,-50,0)
var aMajor = new GO2SXYZ(0.707,0.707,0)
pMajor = center.Added(aMajor)
var z = new GO2SXYZ(0,0,1)
var aMinor = aMajor.CrossProduct(z)
pMinor = center.Added(aMinor)
ellipse = new GO2SEllipse(center, pMajor, pMinor)
ellipse.SetParam(GO2SParam.length, 100)
ellipse.SetParam(GO2SParam.width, 50)

Helix

var center = new GO2SXYZ(50,50,0)
var p = new GO2SXYZ(120,60,0)
var helix = new GO2SHelix(center, p, 10, 5.5, false)
var helix2 =helix.Copy()
helix2.SetParam(GO2SParam.radius, 20)
helix2.SetStepNumber(3)
helix2.SetStepSize(20)

Polygon

var center = new GO2SXYZ(50,50,0)
var p = new GO2SXYZ(120,60,0)
var pl = new GO2SPolygon(center, p, 10, false)
var pl2 = pl.Copy()
pl2.SetParam(GO2SParam.radius, 20)

Profile

var seg1 = new GO2SSegment(new GO2SXYZ, new GO2SXYZ(15,15,0))
var seg2 = new GO2SSegment(seg1.GetLastPoint(), new GO2SXYZ(40,15,0))
var seg3 = new GO2SSegment(seg2.GetLastPoint(), new GO2SXYZ(40,40,0))
var prf =new GO2SProfile(seg1,seg3)
var len1 = seg1.GetLength() + seg2.GetLength() + seg3.GetLength()
var len2 = prf.GetLength()
console.log('geometry length: '+ len1)
console.log('profile length: '+ len2)

Offset

var pt1 = new GO2SXYZ(-10, -30, 0)
var pt2 = new GO2SXYZ(0, 5, 0)
var pt3 = new GO2SXYZ(5, -30, 0)
var seg1 = new GO2SSegment(pt1, pt2)
var seg2 = new GO2SSegment(pt2, pt3)
var prf2 = GO2SProfileCreate.From2Geom(seg1,seg2)
var offsetChamfer = new GO2SOffsetCurve(prf2, GO2SOffsetSide.Outside, 2, GO2SOffsetLink.Chamfer, 1, false);

Hole

var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(10,100,0)
var seg = new GO2SSegment(p1,p2)
// blind hole diam 10, h 20
var hole = new GO2SHole(new GO2SXYZ(),new GO2SXYZ(0,0,1), 10, 20, true);
hole.SetPosition(seg.GetLastPoint());

Solid design

Revolution

var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(20,100,0)
var seg = new GO2SSegment(p1,p2)
var p0 = new GO2SXYZ(80,50,0)
var obl1 = GO2SOblongCreate.FromCenter(p0, 30, 90, 20)
var obl2 = GO2SOblongCreate.FromCenter(p0, 30, 90, 15)
var prf1 = GO2SProfileCreate.FromMultiGeom(obl1)
var prf2 = GO2SProfileCreate.FromMultiGeom(obl2)
var prf = [prf1, prf2]
var axis1 = new GO2SXYZ(GO2SAxis.x)
var revol1 = new GO2SSolid
revol1.CreateMultiRevolution(prf, axis1, 90, false)
var axis2 = seg.GetDirectionOnCurve(0)
var revol2 = new GO2SSolid
revol2.CreateMultiRevolution(prf, axis2, 180, false)

Holes

var pt1 = new GO2SXYZ(30, 0, 0)
var ptc = new GO2SXYZ(40, 0, 0)
var pt2 = new GO2SXYZ(50, 0, 0)
var arc1 = GO2SArcCreate.FromCenterStartEnd(ptc, pt1, pt2)
var arc2 = GO2SArcCreate.FromCenterStartEnd(ptc, pt2, pt1)
var prf = GO2SProfileCreate.From2Geom(arc1,arc2)
var axis = new GO2SXYZ(GO2SAxis.y)
var revol = new GO2SSolid
revol.CreateRevolution(prf, axis, 360, false)
revol.SetColor(0xFF0000)
arc1.SetVisible(false)
arc2.SetVisible(false)
var nbHole=20
var angIncr=3.14/nbHole
for( i=1;i<nbHole;i++)
{
var ang=angIncr*i
var normal=new GO2SXYZ(Math.cos(ang), 0, Math.sin(ang))
var pnt=new GO2SXYZ(50*Math.cos(ang), 0, 50*Math.sin(ang))
var hole=GO2SHoleCreate.SimpleSmoothHole(pnt , normal,5,1,true)
revol.SubtractHole(hole)
}

Boolean operation

var len = 100
var wid = 50
var hc = 40
var ht = 60
var rad = 10
var org = new GO2SXYZ(0,0,0)
var ptX = new GO2SXYZ(len,0,0)
var ptY = new GO2SXYZ(len,wid,0)
var castle = new GO2SSolid
castle.CreateCuboid(org, ptX, ptY, hc)
var tower1 = new GO2SSolid
tower1.CreateCylinder(org, rad, ht)
var mat = new GO2SMatrix
mat.DefineTranslation(new GO2SXYZ(len,0,0))
var tower2 = tower1.Transformed(mat)
mat.DefineTranslation(new GO2SXYZ(0,wid,0))
var tower3 = tower1.Transformed(mat)
mat.DefineTranslation(new GO2SXYZ(len,wid,0))
var tower4 = tower1.Transformed(mat)
castle.MultiUnion([tower1,tower2,tower3,tower4])
castle.SetColor(0xDDDDDD)
castle.SetName('Castle')
var bbox = castle.GetBoundingBox()
var p = bbox[1]
p.SetPosition(bbox[1].X(),bbox[1].Y(),bbox[0].Z())
new GO2SRectangle(bbox[0], p)

Fillets

let solid = GO2SSolidCreate.Cuboid(new GO2SXYZ(), new GO2SXYZ(100,0,0), new GO2SXYZ(100,50,0), 30)
// get all solid faces and find the top face (normal is Z)
let faces = solid.GetFaces()
let top = null
faces.forEach( f => {
if(f.GetNormal().Z() == 1)
top = f
})
// apply a fillet on all edges of the top face
if(top)
{
let edges = top.GetEdges()
solid.ApplyFillet(edges, 5)
}

XYZ computing

Intersections

var center = new GO2SXYZ(10, 20, 0)
var circle = new GO2SCircle(center, 100)
var line = new GO2SLine(new GO2SXYZ(-10, 10, 0), new GO2SXYZ(10, 15, 0))
var intersections = new GO2SIntersect(circle, line)
var inter_array = intersections.GetIntersections()
inter_array.forEach(p => new GO2SPoint(p))

Positions

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)
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)

Tangent and normal directions

// Draw a red arc
geom = GO2SArcCreate.FromCenterStartEnd(
new GO2SXYZ(),
new GO2SXYZ(50,0,0),
new GO2SXYZ(0,50,0)
)
geom.SetColor(0x0000FF)
for(let t=0; t<1.0001; t+=0.2)
{
// draw a yellow point at t position on curve
let pt = geom.GetPositionOnCurve(t)
let point = new GO2SPoint(pt)
point.SetColor(0x000FFFF)
raw_pt = new GO2SXYZ(pt.X(), pt.Y(), pt.Z())
// draw a green segment to show the tangent direction at t position on curve
let dir = geom.GetTangentOnCurve(t)
let seg = GO2SSegmentCreate.FromDirAndLength (raw_pt, dir, 5)
seg.SetColor(0x00FF00)
// draw a blue segment to show the normal direction at t position on curve
dir = geom.GetNormalOnCurve(t)
seg = GO2SSegmentCreate.FromDirAndLength (raw_pt, dir, 5)
seg.SetColor(0xFF0000)
}

Projection

var center = new GO2SXYZ(10, 20, 0)
var circle = GO2SCircleCreate.FromRadius(center, 100)
var pt = GO2SPointCreate.FromCoordinates(-30, 60, 0)
var xyz = circle.GetProjPoint(pt.toXYZ())
var proj = GO2SPointCreate.FromReference(xyz)

Bounding box

var center = new GO2SXYZ(10, 20, 0)
var circle = new GO2SCircle(center, 100)
var bbox = circle.GetBoundingBox()
var line = new GO2SSegment(bbox[0], bbox[1])
var len = line.GetLength()
var len2 = bbox[0].Distance(bbox[1])
console.log('len:' + len)
console.log('len2:' + len2)

Discretization

var center = new GO2SXYZ(50,50,0)
var pMajor = new GO2SXYZ(120,60,0)
var pMinor = new GO2SXYZ(40, 40, 0)
var ellipse = new GO2SEllipse(center, pMajor, pMinor)
var pts = ellipse.Discretize()
pts.forEach(pt => new GO2SPoint(pt));

Translation

var p1 = new GO2SXYZ
var p2 = new GO2SXYZ(10,10,0)
var circle = new GO2SCircle(p1,5)
var seg = new GO2SSegment(p1, p2)
var dir = seg.GetDirectionOnCurve(0)
var circle2 = circle.Translated(seg.GetFirstPoint(), seg.GetLastPoint())

Rotation

var p1 = new GO2SXYZ(10,10,0)
var p2 = new GO2SXYZ(10,100,0)
var obl1 = new GO2SOblong(p1,p2,20)
obl1.SetColor(0X00FF00);
var p0 = new GO2SXYZ
var obl2 = obl1.Rotated(p0, 45, false)
obl2.SetColor(0XFF0000);

Interactions

Colors

// GO2Cam current color is red
var pt1 = new GO2SXYZ()
var pt2 = new GO2SXYZ(10,10,10)
var pt3 = new GO2SXYZ(30,30,30)
var pt4 = new GO2SXYZ(-10,20,-10)
// The Segment will be red
new GO2SSegment(pt1,pt2)
// The Segment will be blue
GO2SGeometryUtil.SetUsedColor(0xff0000)
new GO2SSegment(pt2,pt3)
// The Segment will be red
GO2SGeometryUtil.SetUsedColor(-1) // Back to red
new GO2SSegment(pt3,pt4)
// The Segment will be green
var seg = new GO2SSegment(pt4,pt1)
seg.SetColor(0x00ff00)

Getting entities

// Get all red circle
var redCircles = GO2SGeometryUtil.GetCircle(0x0000FF)
// Change the radius of all red circles
redCircles.forEach(c => c.SetParam(GO2SParam.radius, 20))

3D scene

GO2SInteract.StartSelection()
var geom = GO2SGeometryUtil.GetSelected()
var pts = []
geom.forEach( (g) =>{
if(g.IsPoint())
pts.push(g.toXYZ())
})
var nurbs = new GO2SNurbs(pts, true, -1)
var polyline = new GO2SPolyline(pts, true, -1)
var geom1 = GO2SInteract.PickEntity('Select a linear entity')
if(geom1.IsLinear())
{
var p1 = GO2SInteract.PickXYZ()
var ln = new GO2SLine()
ln.SetParallelByPoint(p1, geom1)
}
else
GO2SDialogUtil.CreateMessageBox('Please select a line or a segment')
var circle = new GO2SCircle(new GO2SXYZ, 50)
var preview = [circle]
for (let i = 0; i < 4; i++)
{
var center = GO2SInteract.PickXYZWithPreview(preview)
new GO2SCircle(center, 50)
GO2SInteract.Redraw() // Update viewport
}

Dialog

LineParam.ui
LineParam.pce

var line_a = GO2SGeometryUtil.GetByName('line_angle')[0]
var line_p = GO2SGeometryUtil.GetByName('line_parallel')[0]
var InitDialog = function()
{
GO2SDialogUtil.SetDouble('angle', line_a.GetParam(GO2SParam.angle))
GO2SDialogUtil.SetDouble('distance', line_p.GetParam(GO2SParam.length))
}
var PushButton_Clicked = function(name)
{
if(name == 'okButton')
{
line_a.SetParam(GO2SParam.angle, GO2SDialogUtil.GetDouble('angle'))
line_p.SetParam(GO2SParam.length, GO2SDialogUtil.GetDouble('distance'))
}
}
var dialog = GO2SDialogUtil.ExecDialog(scriptFilePath+'/LineParam.ui')

Parameters

var start = new GO2SXYZ(0, 20, 0)
var end = new GO2SXYZ(5, -10, 0)
var arc = GO2SArcCreate.FromCenterStartEnd(new GO2SXYZ, start, end)
//1st possibility
console.log('Solution 1')
console.log('angle editable ' + arc.IsParamEditable(GO2SParam.angle))
console.log('rayon editable ' + arc.IsParamEditable(GO2SParam.radius))
console.log('length editable ' + arc.IsParamEditable(GO2SParam.length))
console.log('Width editable ' + arc.IsParamEditable(GO2SParam.width))
console.log('Heigth editable ' + arc.IsParamEditable(GO2SParam.height))
//2nd possibility
var params = arc.GetEditableParams()
console.log('solution 2, editable')
console.log(params)
// Lets assume we designed a cuboid shape and add a fillet on it
// cuboid is labeled 'cube'
// Fillet is labeled in the solid construction tree 'fillet'
var cube = GO2SGeometryUtil.GetByName('cube')[0]
var fillet = GO2SGeometryUtil.GetByName('fillet')[0]
cube.SetParam(GO2SParam.length, 200)
cube.SetParam(GO2SParam.width, 150)
cube.SetParam(GO2SParam.height, 100)
fillet.SetParam(GO2SParam.radius,10)

Files

var filename = 'U:/test.xml';
var file = new GO2SXmlFile(filename, GO2SFileOpen.read_write);
// create nodes
file.CreateRootNode('TestFile');
file.AppendNode('Node1');
file.AppendNode('Node2');
file.GoToChild(); // to Node1
file.GoToSibling() // to Node2
file.AppendNode('SubNode1');
file.AppendNode('SubNode2');
file.GoToParent() // to root
file.AppendNode('Node3');
// write node content
file.GoToRoot() // to root
// write Node1
file.GoToChild() //to Node1
file.SetNodeValue('ValueNode1');
// write Node2
file.GoToSibling() //to Node2
file.GoToChild(); // to SubNode1
file.SetNodeValue('ValueSubNode1');
file.GoToSibling() // to SubNode2
file.SetNodeValue('ValueSubNode2');
file.GoToParent() //to Node2
// write Node3
file.GoToSibling(); // to Node3
file.SetNodeAttribute('att1', 'val1');
file.SetNodeAttribute('att2', 'val2');
file.SetNodeAttribute('att3', 'val2');
file.Save();
file.Close();