Is adding entities in Local or Global coordinates

2017
groups

#1

Probably You will say in Local (inside coordinates of group).but if you are inside of a group I get confused.

OK first outside:

   #I'm choosing a group or component,  let say
group = Sketchup.model.selection[0].definition
   # => #<Sketchup::ComponentDefinition:0x0000000e034a40> 
point1 = group.entities.add_cpoint [0,0,0]

so this point will be added in group’s Local origin

Now inside of the group… component

  # I can do two ways..
entis = Sketchup.model.active_entities
 point2 = entis.add_cpoint [0,0,0]
   # This one will be created in medels Global origin, which I'can understand since I was relating to
   # Sketchup.model.active_entities  ... (even though entis.to_a will show only entities of the active group) 
   # but  if I continue with freshly added point
parent = point.parent
   # I will get exactly the same   #<Sketchup::ComponentDefinition:0x0000000e034a40> like when doing it from outside of the group
point = parent.entities.add_cpoint [0,0,0]
  # this point too is added in Global coordinates

WHY ???

Here is the suprise which I don’t understand. Doing it from inside of the group or outside I was relating to the entities of exactly the same ComponentDefinition. If I would expect any difference in matter of Global vs Local it would be opposite. When outside of group I would expect : Global and when inside : Local.
In https://ruby.sketchup.com/ I didn’t find anything about that… Where Can I.


#2

:sunglasses: pls see this wiki about posting code on the forum:


#3

Short answer: it’s a mess.

Long answer: In the active drawing context and all its parent coordinate systems all coordinates are global. In all other coordinate systems they are locale.

If you for instance make a drawing tool, you only have to think of global coordinates (and sometimes check the Axes object if you want your tool to depend on axes). If you make some code that edits the content of a selected group or component, you only have to think of local coordinates. If you however write code that interacts with or reads the model as a whole regardless of the user’s active drawing context, like a renderer, you may have some problems.

If you traverse the model hierarchy you can quite easily convert coordinates to global space by keeping a reference to the current transformation (see this example). If you need to get the transformation matrices of containers parent to the active drawing context however I think there is no other way than cache the values from before the user enters them.


#4

to MikeWayzovski : thanks… it was sort of surprise to me when I saw it posted on the forum… I edited the code… still I don’t know why there is a bigger red line of text… something to do with those — , but I don’t know the rule for that…


#5

So if I understand well… it doesn’t matter to which group entities I am refering, only matters where in the structure of model’s hierarchy am I (the active drawing context isn’t it)? If I am inside of inside of the ‘n’ group, then making any entities in it and outside of it would be Global. Making changes to groups which are contained by the ‘n’ group whould be Local…

Are then groups some exeptional strange? I mean this:

  # being inside of some group moved, rotated, genereally transformed
ents = Sketchup.active_model.active_entities
group = ents.add_group()
  # this group was added with Global its internal coordinates are aligned with global 
group.entities.add_whatever  # one point not enough
  # and entities to it are added with Global
  
group2 = ents.add_group( group )
  # and that was created with its coordinates aligned to Local

And I was in the same drawing context adding to the same entities… Is it only exeptional behaviour of group created from start with entities or is there a deeper magic beyond that…?
PS
how do you make tableture in those posts?


#6

Edited just here… nobody answered yet
Here is something I’m bothered with… possibly bug cos it behaves strange… in 2017 and 2016

So I was testing with making groups… adding entities… I created a group containing simple qube. rotated it to have difference Global vs Local… I go inside…


ents = Sketchup.active_model.entities # All entities in model

group = ents.add_group()
gents = group.entities
face = gents.add_face([0,0,0], [100,0,0], [100,100,0], [0,100,0])
face.pushpull -100

#group.move! [100,100,100]
point = [100,100,100]
vector = [0,0,1]
angle = 90.degrees
movetrans = Geom::Transformation.translation [0,100,100]
rottrans = Geom::Transformation.rotation(point, vector, angle)
trans = movetrans*rottrans
group.transformation= trans

then I go inside…

# then inside of that group run this 
# I don't know yet how to change active drawing content
# I know it is not a #.model

gents = Sketchup.active_model.active_entities

n=0; cp0 = gents.add_cpoint [n, n, n]
n=100; cp1 = gents.add_cpoint [n, n, n]
n=200; cp2 = gents.add_cpoint [n, n, n]
n=300; cp3 = gents.add_cpoint [n, n, n]
n=400; cp4 = gents.add_cpoint [n, n, n]

group1 = gents.add_group(cp1,cp2,cp3,cp4)
gents1 = group1.entities

group2 = gents1.add_group(cp2,cp3,cp4)

So when I added a first ‘group’ all went beautifull… it just enclosed 4 cpoints in a that group1 which was created in an active drawing context… BEAUTY…
Then I got #.entities of that group1 and created in exactly the same way ‘group2’ passing it 3 cpoints. I thought - beeing pretty naive obviously - that it will create ‘group2’ inside ot first ‘group’ enclousing thouse 3 cpoints. I mean #.add_cpoint, #.add_edges etc would work inside of the group with Local coordinates… but inside…
Surprise
I got group which i wanted, but created in the active drawing context. The ‘group2’ is created with its axes aligned to the local drawing context but cpoints were taken out of ‘group1’ reading their Local Coordinates pasted in active drawing context as it were Global Coordinates and then it was enclosed in a ‘group2’ and placed based on I don’t know what… these are the same antities as I can still use cp4.erase!
But In the ‘group1’ there still are those (rather not those) 3 cpoints but rearranged in the same manner as cp2, cp3 , cp4 in ‘group2’ but not grouped… (it looks like ‘group2’ was pasted in ‘group1’ at local origin and exploded)…

and if thats not strange… it will start now

if I select those new cpoints in the ‘group1’ they don’t highlight blue. instead other not existing cpoint outside of ‘group’ is highlighted blue and greyed out (thats why i think its outside). those new cpoints will highlight blue if i double click it… and that highlight will not dissapear till i doubleclick somewhere else…
in 2016 I didn’t see the blue grayed out phantoms…
The new cpoints in ‘group1’ will dissapear if I save and reopen, but it will crush sketchup on exiting the model… so I think it is a bug…

GOSH… @eneroth3 … looks like your short answer was actually more acurate than long one…

Is it normal or is it a bug… Should this post be moved to bug section?

i decided not to attach file since it doesnt save what I wanted to show


#7

You are creating entities and then moving them into the group. You you observe the same if you first create the groups and then add the entities directly into the final group’s entities? (This is by the way the preferred way to create groups from the API. add_group(existing_entities) have always been a bit fickly.)


#8

I noticed it is fickly… I wanted to group existing entities… dilling now with sketchup 2017 changing entities when exploding groups… I wanted to know very much which entity was which. I’m very much beginner so instead of reading about adding atributes or find_entity_by_pid, I was thinking I could before exploding a group, group its entities all except one. Then after exploding I would know which one was it…
Smiply becouse making a group was what I already knew… at least I thought so… :frowning:
I guess I will just do cp.position to get local position of point inside group and then past it into active drawing context with transformation model.edit_transform …
next day of experiments

man how do you do tabletures here

PS Greate thanks for all plugins… same to eneroth3
I actually am thrilled to write with you…


#9

Can you provide a higher level description of what you are doing?


#10

If You must know I was trying to wrap up some scripts that I wrote as a complete extention so I choose a CLF Extended Views as a sample … It is very simple but as well done in 3 files… didn’t want to make it to easy…
I was working on 2016 but soon enough I found that it was not working in sK 2017… I thought I will take it as an excercise and fix it…
I found the problem was with expoding the group and since 2017 changing entities names while doing it… So I tried very hard to get around it…
I have seen your post on sketchucation site for this plugin so You are maybe familiar with how it works…
I needed to know exactly which entity after exploding the group was which inside the group,. so the views would be properly aligned…

In the end I solved it by not creating groups at all… man that was enoying…
instead I used:

 Sketchup.active_model.edit_transform

so I could create points in opened group’s Local coordinates without the need for creating a group containing another group to align it with local coordinates… that was messy.
And I learned about transformations a bit… and to avoid grouping existing entities cos masters do so too… How come it was not fixed since sketchup 7?

I posted the fixed extention on sketchucation… I couldn’t get direct contact to Chris… He seems upsent…