Skip to content

Commit

Permalink
adding Rhino3dm 8.6.0 samples
Browse files Browse the repository at this point in the history
  • Loading branch information
fraguada committed Apr 12, 2024
1 parent 3a94f8f commit 0562389
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Rhino3dm" Version="8.6.0-beta" />
<PackageReference Include="Rhino3dm" Version="8.6.0" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions rhino3dm/js/node/Samples/AddInstanceDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'fs'
import rhino3dm from 'rhino3dm'

const rhino = await rhino3dm()
console.log('Loaded rhino3dm.')

const model = new rhino.File3dm()
model.applicationName = 'nodejs'
model.applicationDetails = 'rhino-developer-samples'
model.applicationUrl = 'https://rhino3d.com'
model.startSectionComments = 'hello'

const circle = new rhino.Circle(10.0)

const bbox = new rhino.BoundingBox(-10,-10,0, 10,10,20)
const box = new rhino.Box(bbox)
const extrusion = rhino.Extrusion.createBoxExtrusion( box, false )

const objects = []
objects.push(circle.toNurbsCurve(), extrusion)

const attributes = []
const oa = new rhino.ObjectAttributes()
attributes.push(oa, oa)

const point = [0,0,0]
const index = model.instanceDefinitions().add('test', 'an idef', 'https://www.rhino3d.com', 'tag', point, objects, attributes)


const idef = model.instanceDefinitions().findIndex(index)
const xform = new rhino.Transform(100)
const iref = new rhino.InstanceReference(idef.id, xform)
model.objects().addInstanceObject(iref, null)

let opts = new rhino.File3dmWriteOptions()
opts.version = 8
let buffer = model.toByteArrayOptions(opts)
fs.writeFileSync('idef_js.3dm', buffer)




47 changes: 47 additions & 0 deletions rhino3dm/js/node/Samples/AddMaterialWithTexture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as fs from 'fs'
import rhino3dm from 'rhino3dm'

const rhino = await rhino3dm()
console.log('Loaded rhino3dm.')

//create model
const model = new rhino.File3dm()

const b = fs.readFileSync('./rhinologo.png');
const a = new Uint8Array(b)

const ef = rhino.EmbeddedFile.fromByteArray(a)
console.log(ef.length)

const ef_fileName = 'rhinologo.png'
ef.fileName = ef_fileName
console.log('Filename: ' + ef.fileName)
model.embeddedFiles().add(ef)

console.log('how many embedded files: ' + model.embeddedFiles().count)

const texture = new rhino.Texture()
texture.fileName = ef_fileName

const material = new rhino.Material()
material.toPhysicallyBased()
material.physicallyBased().baseColor = { r: 1, g: 0, b: 0, a: 0 }
material.physicallyBased().metallic = 0.7
material.physicallyBased().roughness = 0.5
material.setBitmapTexture(texture)

const index = model.materials().add(material)

const sphere = rhino.Brep.createFromSphere( new rhino.Sphere( [ 0,0,0 ], 10 ) )

const oa = new rhino.ObjectAttributes()
oa.materialSource = rhino.ObjectMaterialSource.MaterialFromObject
oa.materialIndex = index

model.objects().add(sphere, oa)

//save model
let opts = new rhino.File3dmWriteOptions()
opts.version = 8
let buffer = model.toByteArrayOptions(opts)
fs.writeFileSync('materialWithEmbeddedFile_js.3dm', buffer)
39 changes: 39 additions & 0 deletions rhino3dm/js/node/Samples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions rhino3dm/js/node/Samples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "samples",
"version": "0.0.1",
"description": "rhino3dm.js samples to run in node",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"rhino3dm"
],
"author": "Luis E. Fraguada",
"license": "MIT",
"dependencies": {
"rhino3dm": "8.6.0"
}
}
Binary file added rhino3dm/js/node/Samples/rhinologo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions rhino3dm/py/SampleAddInstanceDefinition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#requires rhino3dm.py >= 8.6.0
import rhino3dm

model = rhino3dm.File3dm()
model.ApplicationName = 'python'
model.ApplicationDetails = 'rhino-developer-samples'
model.ApplicationUrl = 'https://rhino3d.com'

# create objects to be in instance definition
circle = rhino3dm.Circle(10)

bbox = rhino3dm.BoundingBox(-10,-10,0, 10,10,20)
box = rhino3dm.Box(bbox)
extrusion = rhino3dm.Extrusion.CreateBoxExtrusion(box, False) #this extrusion is fine

objects = [circle.ToNurbsCurve(), extrusion] #objects need to derive from GeometryBase, hence circle.ToNurbsCurve()

#create the instance definition
oa = rhino3dm.ObjectAttributes()
attributes = [oa, oa]
p1 = rhino3dm.Point3d(0,0,0)
#index = model.InstanceDefinitions.AddInstanceDefinition('test', 'an idef', 'https://www.rhino3d.com', 'tag', p1, objects, attributes)

index = model.InstanceDefinitions.AddInstanceDefinition2( 'test', 'an idef', 'https://www.rhino3d.com', 'tag', p1,(circle.ToNurbsCurve(), extrusion), (oa, oa) )

#add a reference to the Instance Definition to the model
idef = model.InstanceDefinitions.FindIndex(index)
xform = rhino3dm.Transform(100)
iref = rhino3dm.InstanceReference(idef.Id, xform)
model.Objects.AddInstanceObject(iref)

model.Write("idef_py.3dm", 0)
25 changes: 25 additions & 0 deletions rhino3dm/py/SampleAddMaterial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import rhino3dm

model = rhino3dm.File3dm()

ef = rhino3dm.EmbeddedFile.Read2('./rhinologo.png')
print('FileName: ' + ef.Filename)
ef.Filename = 'rhinologo.png'
print('FileName: ' + ef.Filename)
model.EmbeddedFiles.Add(ef)

print('How many embedded files: ' + str(len(model.EmbeddedFiles)))

material = rhino3dm.Material()
material.SetBitmapTexture('rhinologo.png')
index = model.Materials.Add(material)

oa = rhino3dm.ObjectAttributes()
oa.MaterialSource = rhino3dm.ObjectMaterialSource.MaterialFromObject
oa.MaterialIndex = index

sphere = rhino3dm.Sphere(rhino3dm.Point3d(0,0,0), 10)
brep = rhino3dm.Brep.CreateFromSphere(sphere)
model.Objects.Add(brep, oa)

model.Write("material.3dm", 0)
Binary file added rhino3dm/py/rhinologo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0562389

Please sign in to comment.