Donut donut donut :) (but more about Cplane)

Hi everyone,
how can we simply create a point from a “dynamic” cplane in a loop (in python) ?
in the attached exemple, i just try to describe a simple circle while revolving from Z
for making a simple donut shape :slight_smile: , for that i need to orient my Cplane, so i easily do that with a crossproduct… but i still can’t create from the new Cplane at given time before the next loop … i try to keep thinks simple but there… what i have missed ?
thanks for any help (i don’t think that is an hard topic to fix) ^^
TorusModel.py (2.4 KB)

Are you aware of the Torus class in RhinoCommon
Go here http://4.rhino3d.com/5/rhinocommon/ for SDK documentation and search for Torus.

import Rhino
import scriptcontext
import System.Guid

def AddTorus():
    major_radius = 4.0
    minor_radius = 2.0

    plane = Rhino.Geometry.Plane.WorldXY
    torus = Rhino.Geometry.Torus(plane, major_radius, minor_radius)
    revsrf = torus.ToRevSurface()

    if scriptcontext.doc.Objects.AddSurface(revsrf)!=System.Guid.Empty:
        scriptcontext.doc.Views.Redraw()
        return Rhino.Commands.Result.Success
    return Rhino.Commands.Result.Failure


if __name__=="__main__":
    AddTorus()

Hi Menno, :slight_smile:
yes of course the question is more about the construction plane manipulation over nasted loops … Whitch still a useful study when it’s matter to improvise a script with an easy language like Python learning it’s all of what is about :slight_smile:
Meanwhile how do you paste the code in the forum post ?I want to do the same

Hello there,
here a simple exemple (at the end of the script), which cause me some little troubles: it’s sim if like the construction plane was not set by the command

scriptcontext.doc.Views.ActiveView.ActiveViewport.SetConstructionPlane(plane1)  

following the sample script,
thanks.

import scriptcontext
from math import cos
from math import sin
from math import pi
from Rhino.Geometry import Point3d
from Rhino.Geometry import Plane
from Rhino.Geometry import Vector3d
import rhinoscriptsyntax as rs
import Rhino as rh

# Degres to Radians

def degtoRad(deg):
    radValue= (deg*2*pi)/360
    return radValue

# cross Product definition.

def CrossP(vect1,vect2):
    cp = Vector3d(0,0,0)
    cp.X , cp.Y , cp.Z = vect1.Y*vect2.Z-vect1.Z*vect2.Y , vect1.Z*vect2.X-vect1.X*vect2.Z , vect1.X*vect2.Y-vect1.Y*vect2.X
    return cp

# A simpleparametric Vector2D of length 10 and 45 deg from x (by default).

MainDiameter = rs.GetReal("Diameter",20,0.1,100)
MainDiameter = MainDiameter/2
deg = rs.GetReal('Angle ( in deg )',45,0.1,360)

RadialVector = Point3d(cos(degtoRad(deg))*MainDiameter,sin(degtoRad(deg))*MainDiameter,0)-Point3d(0,0,0)
orientationvector = CrossP(RadialVector,Vector3d(0,0,1))

#  CPLANE orientation at the end of an RadialVector.

plane1 = Plane(Point3d(cos(degtoRad(deg))*MainDiameter,sin(degtoRad(deg))*MainDiameter,0),orientationvector)

# Vector Visualisation

rs.AddLine(Point3d(0,0,0),Point3d(RadialVector.X,RadialVector.Y,RadialVector.Z))
rs.AddLine(Point3d(0,0,0),Point3d(orientationvector.X,orientationvector.Y,orientationvector.Z))
VectMidArc = Vector3d(0,0,0)
VectMidArc.X,VectMidArc.Y,VectMidArc.Z = orientationvector.X,orientationvector.Y,orientationvector.Z
VectMidArc.Rotate(pi/4,Vector3d(0,0,1))
arc = rh.Geometry.Arc(Point3d(orientationvector.X,orientationvector.Y,orientationvector.Z),Point3d(VectMidArc.X,VectMidArc.Y,VectMidArc.Z),Point3d(RadialVector.X,RadialVector.Y,RadialVector.Z))
scriptcontext.doc.Objects.AddAngularDimension(rh.Geometry.AngularDimension(arc,5.0))
scriptcontext.doc.Objects.AddArc(arc)

# CPLANE setting.

scriptcontext.doc.Views.ActiveView.ActiveViewport.SetConstructionPlane(plane1)
print scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane()

################################################################################
# There the following torus is not created on Cplane "plane1" but on the world cplane origin Why ?

rs.AddTorus(Point3d(0,0,0),MainDiameter,1.3)

# of course there is an option in the function torus but does the Cplane"plane1"
# SHOULD not be allready set ?

rs.AddTorus(plane1,2,0.1)

# should i must use always this kind of following operation ?

rs.AddPoint(rs.XformCPlaneToWorld(Point3d(0,0,1),plane1))

rs.AddPoint(Point3d(0,0,0))

# so let's see what's is possible to do with just that.

for i in rs.frange(0,2*pi,pi/16):
         rs.AddPoint(rs.XformCPlaneToWorld(Point3d(cos(i)*2.5,sin(i)*2.5,0),plane1))

################################################################################

# World Cplane Reninitialization.
scriptcontext.doc.Views.ActiveView.ActiveViewport.SetConstructionPlane(Plane.WorldXY)