Ruby Component Insertion and Attributes - Not Updating

Ruby Component Insertion and Attributes - Not Updating

Postby placidfury » Sun Jan 18, 2015 2:20 am

Hi all,

I hope this is the right forum to post this in.
I am pretty new to making my own Ruby scripts, so I'm not sure how to do this.
I have some dynamic components I have made, and exported them out of the model as their own .skp files. I want my script to get two points from the user, then load and insert the component at the point specified by the first click, scale it along one axis, then rotate it into position so the second end is at the point specified by the second click. There is also I prompt for the user to set certain values, which are then applied to attributes on the component, as well as the geometry changed.
I have everything working more or less perfectly, with one exception: While the attributes are set on the component, the geometry and textures are not updated on the model until I either edit the components properties manually, or scale it manually.
How would I go about having Ruby update the appearance of the component after all the attributes are changed?
Here's a truncated version/snippet of what I have:
Code: Select all
#load the file
newcomp_def = Sketchup.active_model.definitions.load("Q:\Test.skp")
#create insertion transform
trans1 = Geom::Transformation.new p1
#transform scale along y to correct size
trans2 = Geom::Transformation.scaling p1, 1, (length/12), 1
#move the box over since in my model it's centered and I want it on edge
scootvec = Geom::Vector3d.new (width/2),0,0
#create transform from vector
trans3 = Geom::Transformation.translation scootvec
entities = Sketchup.active_model.active_entities
#insert component at point 1
instance = entities.add_instance newcomp_def, trans1
#scale it to length
instance.transform! trans2
#move it over by half
instance.transform! trans3
#rotate it to the correct angle
instance.transform! transrot
#set attributes
instance.set_attribute("dynamic_attributes", "product", "Name")
instance.set_attribute("dynamic_attributes", "width", width)
instance.set_attribute("dynamic_attributes", "depth", depth)


I have formulas on the component to set the textures, x and z dimension, etc. I have confirmed the values are changed by going into the component editor, the appearance just doesn't change/isn't updated.
Is it simple to accomplish this?

Thanks!

PlacidFury
0
User avatar
placidfury 
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby TIG » Sun Jan 18, 2015 12:22 pm

After the changes you need to force it to regenerate...
Something like this:
Code: Select all
def self.dc_redraw(ent, pbar_visible=true, undo=false)
    ldc = $dc_observers.get_latest_class()
    if undo
        ldc.method(:redraw_with_undo).call(ent, pbar_visible)
    else
        ldc.determine_movetool_behaviors(ent)
        DCProgressBar::clear()
        ldc.method(:redraw).call(ent, pbar_visible)
        DCProgressBar::clear()
        ldc.refresh_dialogs()
    end
    Sketchup.active_model.active_view.refresh
    rescue TypeError => e
    # suppress nil to float conversion error that happens
    # when redraw is called directly with true 2nd arg ?
end#def
Then at the end of your changes use: self.dc_redraw(instance, true, false)
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby placidfury » Mon Jan 19, 2015 10:21 pm

Perfect! Got it going. Thanks!
0
User avatar
placidfury 
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby Pixero » Sat Feb 02, 2019 2:39 pm

Thanks TIG for the redraw DC code.
However I have a question about the speed of the redraw.
For my testing DC component that is fairly simple (a window) it takes approximately 4 to 10 seconds to redraw.
Is that normal?
Is there any way of speeding it up?

Edit:
Here are a few more times for redraw the same DC.
5.579584 seconds to redraw
5.536248 seconds to redraw
4.532769 seconds to redraw
4.360298 seconds to redraw
4.269832 seconds to redraw
9.777517 seconds to redraw
0
User avatar
Pixero 
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby Pixero » Thu Feb 07, 2019 9:14 am

No one knows anything about this?
0
User avatar
Pixero 
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby Pixero » Sun Feb 10, 2019 6:33 pm

If anyone is interested I tried with this code and it's almost instant. :D
Code: Select all
#Redraw to update the DC
dcs = $dc_observers.get_latest_class
dcs.redraw_with_undo(cinst)
0
User avatar
Pixero 
 

Re: Ruby Component Insertion and Attributes - Not Updating

Postby placidfury » Wed May 15, 2019 8:42 pm

Just now saw this - I have been unable to get a batch redraw of DC's to not take significant time. It seems the more DC's you redraw in the same operation, the longer each successive one takes to redraw.
Eventually, I found a way around this by simply scrapping my usage of DC's and switching to groups. Operations on groups are essentially instantaneous.
I'd use DC's if it's one or few objects at a time, or something I want the user to be able to interact with via the DC interface, but most of my plugin's objects are not that - I'm manipulating them with my code. Since I'm doing Engineered Wood Products layouts on very large multifamily buildings, I end up with thousands of objects to manipulate, and DC's simply didn't work well enough, as they were too slow. And the fact that even changing one property - even one that doesn't affect geometry - forces you to have to redraw them each, that time involved was a deal-breaker.
0
User avatar
placidfury 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: tamachan and 6 guests

Visit our sponsors: