The scripting capability introduced in version 2012 is extremely powerful, and my new job is to make sure everyone knows about it and learns how to use it. In this first series of postings we will introduce you to our new scripting technology, walk through the creation of a simple example script, and showcase what is possible with scripting"out of the box."

First you have to realize that I'm coming in fresh from a short, but much needed, four day vacation. Well, two and a half days if you subtract my remote monitoring of emails for the first 32 hours. Call it passion, habit or a deep rooted fear of detachment, I have a problem unplugging. Luckily I have a caring support group here at Geomagic and at home. Being ribbed by e v e r y o n e it finally sunk-in...

Arriving early Monday morning, I moved my personal effects to my new cubicle, said goodbye to my old Dell space heater, hello to my new rockin' laptop and twin 24" monitors, and with much anticipation, assumed my new role as Development Support Specialist and Evangelist. What does that mean? In a nutshell: scripting and automation, or rather, extending our products through programmatic solutions. If you understand that, or even if you don't, give me a call or send me an email through Support to find out more.

This first script will be straight forward. The idea is to introduce some basic concepts that can be built upon as you begin developing your solutions with Geomagic Studio 2012, Geomagic Qualify 2012, Geomagic Wrap 2012, or Geomagic Qualify Probe 2012. Future postings will delve into more powerful and complex concepts, so keep an eye open for upcoming posts. Before we get started writing code I have to say that we are not a CAD system; we are built upon points and polygons. In comparison to CAD, some of our abstractions and methods might seem unconventional or overly basic at first, but over time we will build a library of tools that will make common tasks much easier. That is a major strength of scripting, or programming in general for that matter. Let's just dig in and you'll get the idea.

Scripting out of the box essentially our first example will be to create a cube (or"box", if you will) and remove or cut a sphere from its volume. You can look at the resultant model here in 3D PDF format.

The first step is to build the cube. We could do this a number of ways. I chose to start with a feature plane, convert it to a polygon mesh, and then extrude the boundary and close the end. The next step is to create a feature sphere, convert that to a polygon mesh, and remove it from the cube with a Boolean cut. This is what we're shooting for

OutOfTheBox_Image_002.PNG

For those of you that can't wait for the next post, the final script can be cut and pasted from below or you can download it here. I suggest you download the file and save it to the default macro directory. On my laptop it is:

C:\Users\Public\Documents\Geomagic\Geomagic Studio 2012\macros

Or you can save it someplace that is convenient to browse to like your desktop or Documents folders. Then simply open it from the Scripting Panel's file menu. Take a look at the script, and start exploring the online Help. You can find it in the Geomagic install folder (again, on my computer):

C:\Program Files\Geomagic\Geomagic Studio 2012\help\platform\index.html

Just open this file in your favorite browser and start at the top with Scripting Language. This is an important first step. Although Python 3.0 has been released, we are using Python 2.7, you don't need to install anything Python related, yet...there will be much more on that later. If you have browsed the online Python documentation our Help should look familiar. We use the same technology (Sphinx) to build our online documentation as they do. One outstanding feature of Sphinx is that anything we expose through Python for you to use in scripts is autmatically documented and made available in Help. This Help is our official and comprehensive documentation set for scripting and I use it on a daily basis. The cut and paste"working" code examples are a great learning tool, but they also allow me assembly scripts quickly. I keep a shortcut to Help on my desktop for convenience.

Over the next few posts we will break this script down, walk through every step I took to develop the code from scratch, explain some basic concepts, and introduce more advanced objects, modifiers, and techniques. I will also share with you the resources online that I found helpful when learning Python and our API and still find useful as I write and debug my solutions.

Stay tuned for more and get coding!

Richard

Go to Part Two

Richard Sandham | Development Support Specialist and Evangelist

office: +1 800-691-1839 | support +1 800-251-5551 | fax +1 919-474-0216


Here is the script...

# --------------------------------------------------------------------------------
# Geomagic, Inc - Scripting API - Sample Script
# Title: Scripting Out of the Box - Part 1
# Script File: OutOfTheBox.py
# Author: Richard Sandham
# Date: 11/06/2011
#---------------------------------------------------------------------------------
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR GEOMAGIC, INC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#
# --------------------------------------------------------------------------------
#
# Imports
#
import geoapiall
for m in geoapiall.modules: exec"from %s import *" % m in locals(), globals()
import geoappall
for m in geoappall.execStrings: exec m in globals(), locals()
#
# Debug switches
#
_gDebug = 0 # set to 0 to: remove verbose debug console comments
_gGeomDebug = 0 # set to 0 to: not add construction geometry to the MM
_gRemoteDebug = 0 # set to 0 to: turn off remote debugging
#
# Remote debugging with Eclipse
#
if _gRemoteDebug: import pydevd
if _gRemoteDebug: pydevd.settrace()
#
# Global variables
#
sideLength = 1.0
sphereDiameter = 1.38
#
# Create plane
#
plane1 = PlaneFeature()
plane1.normal = Vector3D(0.0, 0.0, 1.0)
plane1.origin = Vector3D(0.0, 0.0, sideLength/2)
plane1.uDir = Vector3D(1.0, 0.0, 0.0)
plane1.uRange = Vector2D(-sideLength/2,sideLength/2)
plane1.vRange = Vector2D(-sideLength/2,sideLength/2)
if _gDebug: print u"plane1:", str(plane1)
#
# Create and run a feature modifier to convert our plane to a mesh
#
createMeshFrom3DFeature = CreateMeshFrom3DFeature()
createMeshFrom3DFeature.feature = plane1
createMeshFrom3DFeature.run()
#
# Extract the mesh from the modifier
#
planeMesh = createMeshFrom3DFeature.mesh
if _gDebug: print u"planeMesh:", str(planeMesh)
#
# create an edge selection and mark the boundary of the mesh
#
esel = EdgeSelection(planeMesh)
esel.markBoundary(True)
#
# extrude the boundary of our plane in the negative z direction
#
extrude = ExtrudeHeight(planeMesh)
extrude.height = 1.0
extrude.direction = Vector3D(0, 0, -1)
extrude.closeExtrusion = True
extrude.run(esel.first()) # esel.first()
#
# Deselect all triangles after the extrude
#
geoapp.clearActiveTriangleSelection(planeMesh)
#
# Create the Sphere
#
dSphere = SphereFeature()
dSphere.center = Vector3D(0.0, 0.0, 0.0)
dSphere.radius = sphereDiameter/2
dSphere.axis = Vector3D(0.0, 0.0, 1.0)
dSphere.radDir = Vector3D(0.0, 1.0, 0.0)
dSphere.latitudeRange = Vector2D(0.0, 1.570796)
if _gDebug: print u"dSphere:", str(dSphere)
#
# Convert the sphere to a mesh
#
createMeshFrom3DFeature = CreateMeshFrom3DFeature()
createMeshFrom3DFeature.feature = dSphere
createMeshFrom3DFeature.maxDeviation=.001
createMeshFrom3DFeature.run()
#
# Extract the mesh
#
dMesh = createMeshFrom3DFeature.mesh
if _gDebug: print u"dMesh:", str(dMesh)
#
# Boolean the sphere mesh from the cube nesh
#
meshBoolean = BooleanMesh(planeMesh)
meshBoolean.meshModelAdd = dMesh
meshBoolean.operation = meshBoolean.Subtract1
meshBoolean.run()
#
# Add the final model to the model mgr
#
geoapp.addModel(planeMesh, u"OutOfTheBox")
#
# Force a redraw to update the graphics
#
geoapp.redraw(vero)
#
#  End of File