Beginning Ruby - Array and other Questions

Beginning Ruby - Array and other Questions

Postby BryanHL » Mon Apr 17, 2017 2:39 pm

Hi,

I am just learning Ruby...

Given

arrIcosaVtxs = Array.new(12, Array.new(3))

I can assign like this

Code: Select all
  arrIcosaVtxs[0] = [Geom::Point3d.new(0,   0,   0),
                     Geom::Point3d.new(1.m, 0,   0),
                     Geom::Point3d.new(0,   1.m, 0)]


but this

Code: Select all
  arrIcosaVtxs[0, 0] = [Geom::Point3d.new(0,   0,   0)]
  arrIcosaVtxs[0, 1] = [Geom::Point3d.new(1.m, 0,   0)]
  arrIcosaVtxs[0, 2] = [Geom::Point3d.new(0,   1.m, 0)]


fails here

component.entities.add_face(arrIcosaVtxs[0]) ; # returns a Face

I am happy to use what works, just wondering why...

Q2
I lifted the add_face code from a tutorial.

In the tute the author assigned the result to a variable.

newface = new_comp_def.entities.add_face(points) ; # returns a Face

I have removed the assignment assuming I can access a face in the entities later if need be?

Q3
Also, is it possible to create all my geometry and add to a component later?

Q4
And possible to make a created component appear in the tray?

thx...
0

BryanHL 
 

Re: Beginning Ruby - Array and other Questions

Postby TIG » Mon Apr 17, 2017 4:31 pm

Q1:
Your failing part is setting the array's three elements to three separate arrays - each containing one element [a point].
The working version is setting the array to contain three elements - each being a point.
A working version for the failing part would be:
Code: Select all
arrIcosaVtxs[0, 0] = Geom::Point3d.new(0, 0, 0)
arrIcosaVtxs[0, 1] = Geom::Point3d.new(1.m, 0, 0)
arrIcosaVtxs[0, 2] = Geom::Point3d.new(0, 1.m, 0)


Q2:
You don't need to make a reference to that face as you create it.
However, it does no harm.
Unless it'll be easy to retrieve a reference to that face later on - e.g. it's the only one in that entities collection...

Q3:
You normally add the geometry into and entities context, so why would you want to add it into one context and move it into another ?
Normally you would add the geometry directly into the component definition's entities collection.
The only exception to this is if you have a selection of entities [in the current active_entities] and you add a group made from those - that group must also be in the same active_entities... otherwise it'll bugsplat!
group = model.active_entities.add_group(model.selection.to_a)
After that you could convert the group into a component instance and rename its definition as desired...

Q4:
Any new definition you add to the model should appear in the Component Browser's model tray...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Beginning Ruby - Array and other Questions

Postby BryanHL » Mon Apr 17, 2017 4:41 pm

I can answer my first question myself

In another language,

Code: Select all
    arrIcosaVtxs = Array.new(12, Array.new(3))

    arrIcosaVtxs[0, 0] = (Geom::Point3d.new(0,   0,   0))
    arrIcosaVtxs[0, 1] = (Geom::Point3d.new(1.m, 0,   0))
    arrIcosaVtxs[0, 2] = (Geom::Point3d.new(0,   1.m, 0))

would be setting the array second dimension elements

Just realised that in Ruby, the second digit after the comma above is a count...

Tried this and it works

Code: Select all
    arrIcosaVtxs[0][0] = (Geom::Point3d.new(0,   0,   0))
    arrIcosaVtxs[0][1] = (Geom::Point3d.new(1.m, 0,   0))
    arrIcosaVtxs[0][2] = (Geom::Point3d.new(0,   1.m, 0))
1

BryanHL 
 

Re: Beginning Ruby - Array and other Questions

Postby BryanHL » Mon Apr 17, 2017 5:06 pm

Hi TIG,

I just posted re my Q1 when I realised what I was doing wrong...

Q2:
You don't need to make a reference to that face as you create it.
However, it does no harm.
Unless it'll be easy to retrieve a reference to that face later on - e.g. it's the only one in that entities collection...


There will be many faces. I am creating polyhedrons.

You used the word "collection" which was the reason for my post. Is the entities collection an ordered list? As in, if I put 5 in, I can get the 2nd one out from the 2nd position?

Q3:
You normally add the geometry into and entities context, so why would you want to add it into one context and move it into another ?


I dunno, just learning...
I was wondering if the geometry can be created separately. But maybe the methods are only available through the entities context?

Lets say I make a face into a component. I have multiple instances of that component, and I want to add them all to a separate single instance component at the end...

Q4:
Any new definition you add to the model should appear in the Component Browser's model tray...

Thx, I didn't notice it there bcoz no name...
0

BryanHL 
 

Re: Beginning Ruby - Array and other Questions

Postby TIG » Mon Apr 17, 2017 6:26 pm

You can't rely on the ordering of a selection or an entities collection.
Also you should never 'loop' through a selection or an entities collection, IF your code will affect the selection or collection - since it's references will 'shift' - use .to_a to free it as an array if you must do this...
If you are adding several faces you make a simple empty array at the start:
faces = []
Then every time you make a face you can add that into that array.
...
face = ...
faces << face
...

Later on you can access the faces array and its ordering will remain constant.

You can only add geometry into an entities context - so that's either the model.entities [the lowest base level - even if not currently active], model.active_entities [current active context - which might be any collection], or any group.entities or definition.entities ...

In passing, a group is just a special subset of a component.
They both have a definition [so does an Image, but we'll not go there yet].
A definition can have instances - placed into some entities context.
A component definition can have several instances - changing one changes the others.
You can make an instance unique so that editing does not affect its relatives.
A group definition can have several instances, BUT editing one of them automatically makes it unique.
When iterating a definitions collection you can filter its members using defn.image? and defn.group? to choose or avoid those types...
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: blue98, maccrock, TIG and 9 guests

Visit our sponsors: