Entity Attribute !?
-
im trying to modify a plugin to have more features. im noob so i might be wrong.
i added an array that store "Entities Attributes".
i need to deal with the Entities.
when i use "Entities[0].erase!" it works.
but when i retrieve the "Entities Attributes" and use it with ".erase!" does not work.
i need way for retrieving the Entities it self from "Entities Attributes" or a way for dealing with Entities by "Entities Attributes".im using rubyeditior :
E.g Entity: #Sketchup::Edge:0xaa527e0
if u have a website that has all thees inf i will be glad.i searched google and dev website couldnt find what i want. thx for help anyway.
-
I'm sorry, but what do you mean by "Entities Attributes"? I'm a bit confused if you are dealing with an Entities collection, a sub-class of an Entity or an attribute here.
You mentioned: "Entities[0].erase!"
What is "Entities" here? Capital letter indicate class/module or constant. But is that the name of your array?Do you have a code snippet showing what you are trying to do?
-
sry for that. its "entities[0].erase!" no Capital letter
thiss pic for what i mean by Entities Attributes.and this one is a box message for myarray.to_s contents.
and thiss the code i want to add : for deleting array content i might need use each i dont know but i tried use 1 content does not works.
-
What is the plugin that you want to modify, what does it do and what exactly do you want to achieve in your modification?
This would be good to know so we can help because the example has several issues:
- Whatever plugin it is, it uses global constants
$MTtool_edg
(which is so bad it's even banned from Extension Warehouse). - it uses convoluted half-abbreviations. In Ruby, human language and meaningful names are preferred, rather not abbreviations, and in no case anything in between. Consistency is all in programming. Bad naming leads to unmaintainable code and errors.
- your added method accesses that global constant that has been set from outside. That is risky because you don't have control over what value it currently has. It is the best example of a side effect and side effects are better to avoid. There is certainly a better way how we can achieve what you want.
Have you discovered the official API documentation, where you can browse hierarchically what you need? For example you have a Sketchup::Edge and you go to that class (and also "parent" on that page) and see everything you can do with an edge.
- Whatever plugin it is, it uses global constants
-
Learn basic Ruby before using the SketchUp API.
-
thx all. but that didnt help. i know about $ global var. but i need to use the var inside 2 classes. 1st class for store the entity, 2nd to modify it. thiss how the plugin work i dont want to modify whole plugin. im not interested in sharing on EXTENSION Warehouse. i only do that for my own use.
i will try the Resources u provide. thx for ur time anyway. -
@mtriple said:
i added an
Array
that store "Entities Attributes".... but when i retrieve the "Entities Attributes" and use it with "
.erase!
" does not work.Because,
erase!()
is aSketchup::Drawingelement
instance method (inherited by subclasses.)The Ruby standard class
Array
, is NOT a subclass ofSketchup::Drawingelement
, so it does not inherit anerase!()
method.However, class
Array
has it's owndelete_at()
method.
So you can useents_array.delete_at(0)
to erase the first array member.Or... you can use
ents_array.shift
, which erases the 1st member but returns it from the method call. This can be used to store the 1st member in another reference. Like:
item1 = ents_array.shift
-
model = Sketchup.active_model
Then
entities = model.entities
OR
entities = model.active_entities
Gives you a reference to a 'collection' of entities.
Do not iterate that collection in a way that removes items, because it dynamically changes the collection so you get unexpected results: instead you need to 'freeze' it as a snap-shot array first:
ents = entities.to_a
Then you can erase any element in the array by iteration - although I recommend you test for its validity beforehand - for example in your array you have some previously deleted entities, and erasing an edge deletes its face you might have not looked at yet:
entities.to_a.each{|e| e.erase! if e.valid? }
Which will avoid errors.
You can more efficiently delete a whole set of entities in one go, thus:
entities.erase_entities(entities_to_a)
or even the blunt:
entities.clear!
If you pre-process the array that is passed - so perhaps you only delete the faces - you limit the deletions, thus:
entities.erase_entities(entities.grep(Sketchup::Face))
...
Please learn how to filter and process array lists... -
thx u all. that was useful ill replay since i do some changes.
Advertisement