Beginning Ruby - Array and other Questions
-
Hi,
I am just learning Ruby...
Given
arrIcosaVtxs = Array.new(12, Array.new(3))I can assign like this
`
arrIcosaVtxs[0] = [Geom;;Point3d.new(0, 0, 0), Geom;;Point3d.new(1.m, 0, 0), Geom;;Point3d.new(0, 1.m, 0)]`
but this
`
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 FaceI 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 FaceI 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...
-
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: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... -
I can answer my first question myself
In another language,
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
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)) -
Hi TIG,
I just posted re my Q1 when I realised what I was doing wrong...
@unknownuser said:
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?
@unknownuser said:
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...
@unknownuser said:
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...
-
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 anygroup.entitiesordefinition.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...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement