Grasshopper

algorithmic modeling for Rhino

Hello everybody

I am whiting code in python according C# version:

Rhino.DocObjects.Tables.LayerTable layers = doc.Layers;
int index = layers.Find("AngleDimention1", false);
if(index == -1){
Rhino.DocObjects.Layer layer = new Rhino.DocObjects.Layer();
layer.Default();
layer.Color = Color.FromArgb(230, 230, 20);
layer.Name = "AngleDimention1";
layers.Add(layer);
index = layers.Find("AngleDimention1", false);

But There is something wrong in first line:

layers=Rhino.RhinoDoc.ActiveDoc.Layers()
index=layers.Find("aaa",false)
if index==-1:
layer=Rhino.DocObjects.Layer()
layer.Default()
layers.Add(layer)
index=Find("aaa", false)

Runtime error (ArgumentTypeException): LayerTable is not callable
Traceback:
line 6, in script

somebody can help me?Thank you very much!

Views: 2285

Replies to This Discussion

Try to remove the parentheses:

layers = Rhino.RhinoDoc.ActiveDoc.Layers

Thank you very much,done it 

and Another question:

Here is a code in Rhino.common,But does't work in GH:

import Rhino
import scriptcontext
import System.Drawing.Color

def AddChildLayer():
# Get an existing layer
default_name = scriptcontext.doc.Layers.CurrentLayer.Name
# Prompt the user to enter a layer name
gs = Rhino.Input.Custom.GetString()
gs.SetCommandPrompt("Name of existing layer")
gs.SetDefaultString(default_name)
gs.AcceptNothing(True)
gs.Get()
if gs.CommandResult()!=Rhino.Commands.Result.Success:
return gs.CommandResult()

# Was a layer named entered?
layer_name = gs.StringResult().Trim()
index = scriptcontext.doc.Layers.Find(layer_name, True)
if index<0: return Rhino.Commands.Result.Cancel

parent_layer = scriptcontext.doc.Layers[index]

# Create a child layer
child_name = parent_layer.Name + "_child"
childlayer = Rhino.DocObjects.Layer()
childlayer.ParentLayerId = parent_layer.Id
childlayer.Name = child_name
childlayer.Color = System.Drawing.Color.Red

index = scriptcontext.doc.Layers.Add(childlayer)
if index<0:
print "Unable to add", child_name, "layer."
return Rhino.Commands.Result.Failure
return Rhino.Commands.Result.Success

if __name__=="__main__":
AddChildLayer()

There's a quite a few things to be aware of when using Python in GH versus in Rhino. For your script this is mainly that you are calling methods which aren't used/supported by GH! This being all the # Prompt the user to enter a layer name stuff. In GH you could instead reference the name of a layer by feeding a string into the Python component and use the name of this variable in the script. Secondly, when you wan't to create the layers in Rhino you have to manually change the scriptcontext from the GH document to the Rhino document, see Guilios reply in this thread: http://www.grasshopper3d.com/forum/topics/selecting-geometry-proble....

Thank you very much!It is very helpful

No worries. Come to think of it you might actually want to check out the RhinoScriptSyntax functions for doing this kind of stuff. Should make it a bit easier than using RhinoCommon if your just starting out with Rhino.Python: http://www.rhino3d.com/5/ironpython/modules/layer_module.htm

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service