sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    DC instance make unique

    Scheduled Pinned Locked Moved Developers' Forum
    22 Posts 3 Posters 622 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jolranJ Offline
      jolran
      last edited by

      Hi. I have this snippet, trying to make a nested DC unique so I can manipulate it without worries.
      And it's a copy of a DC in current model selection.

      #comps = active selected component in Sketchup
      
      	comps_def=comps.definition
           comp_tr=comps.transformation 
           ### Make an instance of comps with comps transformation ###
      	  comps_instance=ents.add_instance(comps_def, comp_tr)
             comps_instance.make_unique   
              comp_ent=comps_instance.explode   # this crashes sketchup!
      

      The instance doesent get unique? Is there any problem of using original components transformation?
      I used it to position the 2nd instance to match the original. πŸ˜•

      1 Reply Last reply Reply Quote 0
      • TIGT Online
        TIG Moderator
        last edited by

        I'm still not entirely sure why it has to be a DC - why not just a component ?
        If it's a DC whenever you add an instance of it it's already made unique! It's a DC thing. Any Components inside it are unlikely to be though...

        ❓

        TIG

        1 Reply Last reply Reply Quote 0
        • jolranJ Offline
          jolran
          last edited by

          Hi TIG.

          @unknownuser said:

          I'm still not entirely sure why it has to be a DC - why not just a component ?

          Since you know what I'm working on a.t.m. Are you refering to why I'm using DC's or why DC are the cause of this problem?

          But yes it is the components inside that's the problem. Actually, I really do not need the additional instance. I'm just after the face and edge collection from the components inside the DC.

          Iterating through nested component entities I get perculiar results.

          If putting the DC inside a group and explode the group, it crashes sometimes. Although if I iterate the group.entities and explode, it doesent crash. BUT only 1 component inside get's exploded that way 😲 . Using group.ents.to_a or not doesent matter. Neither "next if not e.valid?" or something like that.

          If you have a better way to get to the face or edge entities inside the nested components without exploding them, I would be most greatful to hear about it πŸ˜„

          Then I wouldent have to do this method and the original DC could be left in peace.

          1 Reply Last reply Reply Quote 0
          • TIGT Online
            TIG Moderator
            last edited by

            Please explain [simply] why you are using a DC as opposed to an ordinary Component...

            TIG

            1 Reply Last reply Reply Quote 0
            • jolranJ Offline
              jolran
              last edited by

              Well I'll try.

              TIG, you pretty much know what I'm after. It's for the stamp tool.

              So for ex let's say I have a pattern of concrete with triangles. These triangles should be adjustable in size. Also when I scale up or down to cover the faces of various size, I don't want the scaling to affect the interior components. Main concepts of DC.

              I think I know what you are getting at though. I wasent specific enough when I said "stamp tool".
              Maybe you thought the user would just stamp a component and move it around and stamp again? One could do that as well.
              But it can be painful to align advanced patterns. Better of, cover the whole face. Therefore DC.

              I hope this makes things clearer? πŸ˜„

              1 Reply Last reply Reply Quote 0
              • TIGT Online
                TIG Moderator
                last edited by

                You could scale instances of any component using a [random] transformation ?
                I'm still not clear what you are trying to do with this.

                If you look at AutoCAD concrete hatching it is NOT random.
                It is a 'tiled' set of random looking line patterns, so why not make a big tile-able concrete-hatching component of edges, add it and then repeat it X/Y and then explode all, intersect t with a face, then trim to remove unwanted edges and finally remove all residual faces ?
                there are no CAD PATTs that are random they all tile, some just have a bigger 'repeat' to fool you... πŸ˜’

                TIG

                1 Reply Last reply Reply Quote 0
                • jolranJ Offline
                  jolran
                  last edited by

                  Gulp 😲 I did not think of making them only tileable. I'm a disaster...

                  I was stuck in my mind thinking of DC to do it all.

                  OK so from this on, if things go better. Step 1 make "manual" stamp tool. Step 2 "inbuilt" library, like your 2dtools hatches. So the rule here IF the user wants to make own patterns, is NO subcomponents?

                  I don't think it's a good idea to ditch the manual stamp tool idea. That could be useful as well, for precision.

                  Alright, will try what you say. Thanks TIG for putting me on the right track. πŸ‘

                  1 Reply Last reply Reply Quote 0
                  • jolranJ Offline
                    jolran
                    last edited by

                    Ehum, actually the problems remain using non nested components. If I tile components there are multiple, no?
                    The problem was exploding nested components. Now it is exploding multiple components.

                    If I put the components inside a group and explode the group.entities(iterate) only 1 component get's exploded.

                    I get constant splats following advice on this page. http://forums.sketchucation.com/viewtopic.php?f=180&t=38925&p=343823&hilit=group+from+selection#p343823

                    This is what I do: (first I iterate through selection and add to arrays: faces[], edges[], componentinstances[])

                    Then

                    #comps=array of componentinstances
                    	
                    	      gp2=ents.add_group(comps)
                              cents=gp2.entities
                               cents.each{|e|   # does NOT like gp2.to_a CRASH!
                    	      next if not e.valid?
                    		  e.explode if e.class==Sketchup;;ComponentInstance 
                    	   }
                    

                    Probably doing some fundamential misstake, but cannot find any relevant info about this particular problem..
                    I can only imagine it has to do with the fact that I am adding several components to the group.
                    OR maybe it doesent work to run instances through an iterator? The iterator think's the instances are the same object? BUG?
                    That makes no sence though... πŸ˜•

                    1 Reply Last reply Reply Quote 0
                    • jolranJ Offline
                      jolran
                      last edited by

                      I got it. Stupid missunderstanding. 😍

                      If comps is an array of entities I add to the group. It's THEM I should explode. Not give the group entities a new name and
                      try to explode that object.

                      
                      	if comps
                                gp2=ents.add_group(comps)
                                comps.each{|e|   # does NOT like gp2.to_a CRASH!
                      	     next if not e.valid?
                      		  e.explode if e.class==Sketchup;;ComponentInstance 
                      	   }
                      

                      It's funny I always find the solution 1 min after typing it on this forum.

                      1 Reply Last reply Reply Quote 0
                      • TIGT Online
                        TIG Moderator
                        last edited by

                        Never use ents.add_group() with amy objects within the ()... UNLESS ' ents' is the active_entities.
                        If it's not then you must add objects to the ents AFTER creating the group as an 'empty group ()'...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • jolranJ Offline
                          jolran
                          last edited by

                          Ok, thanks for reminding me. It even say so in the API, groups should not be created that way.
                          But I don't know how to do otherwise, if not creating some new entities..

                          So how do I do it in this case. gp2=ents.add_group(). How do I add comps to gp2? comps=gp2.entities will not work will it?

                          1 Reply Last reply Reply Quote 0
                          • TIGT Online
                            TIG Moderator
                            last edited by

                            gp=ents.add_group()

                            assuming comp1 is a component-instance

                            comp2=gp.entities.add_instance(comp1.definition, comp.transformation)

                            comp1.erase! so it's 'moved' into the group.

                            OR if it's just a definition and you have a transformation called 'tr'...

                            comp=gp.entities.add_instance(definition, tr)

                            IF it's s group it's slightly more convoluted to move it into a group that's not in the active_entities...

                            gp=ents.add_group()
                            gp2=gp.entities.add_instance(gp1.entities.parent, gp.transformation)
                            gp1.erase!
                            gp1=gp2

                            you have effectively moved gp1 into gp...

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • jolranJ Offline
                              jolran
                              last edited by

                              Thank's a lot TIG! This is new to me and very useful. This only work's with 1 object, no? But I might have multiple components in an array. I will investigate..

                              Strange I haven't picked this up earlier, been reading this forum very much lately. There is so much to learn...

                              1 Reply Last reply Reply Quote 0
                              • jolranJ Offline
                                jolran
                                last edited by

                                This did the trick! I mean created multiple instances inside group.

                                if comps
                                           gp2=ents.add_group()
                                		    cents=gp2.entities
                                             comps.each{|e|
                                             		 
                                	         cents.add_instance(e.definition, e.transformation) 
                                	   }
                                	        cents.each{|e|  
                                	        #if e.class==Sketchup;;ComponentInstance #obselete?
                                	         
                                            e.explode
                                	   }
                                        end
                                
                                

                                Edited: Aahh heck... Faced with the same problem again. Can only explode 1 instance in the group..
                                And if I explode the group gp2.explode it crashes. Added the change to the snippets..

                                1 Reply Last reply Reply Quote 0
                                • jolranJ Offline
                                  jolran
                                  last edited by

                                  Yeyy! Now I have exploded all instances inside the group. Changed from cents.each to cents.to_a.each

                                  I know about that method. Stupid did not think about doing that earlier...

                                  Main thing here. Working with instances it is important to get them exploded?

                                  Even if I added 2 instances to gp .Exploded 1 of the instances. Then made a group copy like gp1=gp.copy.
                                  I got splats erasing first group called gp.
                                  Now when every entities is exploded, I can remove the original group safely after copying it.

                                  1 Reply Last reply Reply Quote 0
                                  • TIGT Online
                                    TIG Moderator
                                    last edited by

                                    Stop iterating entities collections that you will be changing during the iteration !
                                    Fix the list with entities.to_a then all is well even when you explode/erase or otherwise change things... πŸ˜„

                                    TIG

                                    1 Reply Last reply Reply Quote 0
                                    • jolranJ Offline
                                      jolran
                                      last edited by

                                      Yes πŸ˜„

                                      Anyway. Now it's possible to collect edges, faces even materials etc. Nice!

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by

                                        @tig said:

                                        If it's a DC whenever you add an instance of it it's already made unique! It's a DC thing.

                                        This is not true in manual mode. Adding another instance from the Components dialog, does just that.
                                        Afterward... editing one of then, edits them all.

                                        So.. NEW Question:

                                        I do not see a dedicated boolean API method to test a Group or ComponentInstance for uniqueness.

                                        I suppose it would be ...
                                        obj.definition.instances==0
                                        for a ComponentInstance

                                        .. and for a Group:
                                        model.definitions.any?{|d| d.group? && d.instances.length==0 && d.instances.include?(obj) }


                                        I wonder if any of us would like a way to always force a definition to clone itself, so that all instances are always unique.
                                        maybe a force_unique attribute?

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • jolranJ Offline
                                          jolran
                                          last edited by

                                          @unknownuser said:

                                          I do not see a dedicated boolean API method to test a Group or ComponentInstance for uniqueness.

                                          Interesting reading.

                                          That confuses me how groups work in SU. It feels like they are Component "light", where they logically(in my head) should only be a group of geometry, and not more than that.

                                          I guess uniqueness has an importance when working with nested DC's. Sometimes find them hard to explode.

                                          1 Reply Last reply Reply Quote 0
                                          • Dan RathbunD Offline
                                            Dan Rathbun
                                            last edited by

                                            @jolran said:

                                            That confuses me how groups work in SU.

                                            Sometimes I confuse myself, as well.

                                            @unknownuser said:

                                            ](http://code.google.com/apis/sketchup/docs/ourdoc/group.html)":1yyw26bz]Groups in SketchUp are very similar to Components, except that there is no instancing of groups. That means that you always will have one and only one of each of your groups.

                                            Now that's not correct.

                                            1. Take an empty model. (It helps to purge the "person" component.)
                                            2. Draw a cube, and group it.
                                            3. Then select it, and choose "Copy" from the Edit menu.
                                            4. Then select Paste from the edit menu, and put the copy somewhere.
                                            5. Do it again, so there are 3 of them.
                                              Then in the console:
                                              d = Sketchup.active_model.definitions d[0]

                                            #Sketchup::ComponentDefinition:0x6227580
                                            d[0].group?
                                            true
                                            puts d[0].instances
                                            #Sketchup::Group:0x62487bc
                                            #Sketchup::Group:0x62487a8
                                            #Sketchup::Group:0x6248794

                                            Of course, they share the same primitives...
                                            g = d[0].instances g[0].entities.to_a == g[1].entities.to_a

                                            true

                                            @jolran said:

                                            It feels like they are Component "light", where they logically (in my head) should only be a group of geometry, and not more than that.

                                            Your on the correct track ....

                                            @unknownuser said:

                                            ](http://code.google.com/apis/sketchup/docs/ourdoc/group.html)":1yyw26bz](In the actual implementation, SketchUp keeps track of groups as a special kind of Component that combines properties of definitions and instances, ...

                                            If you compare the method lists, between Group and ComponentInstance, you'll see they have nearly the same methods.
                                            The difference? A Group cannot be saved out to a file (directly) for use in other models, and it's easier to get at a ComponentInstance's definition, because there's an API method for it.

                                            I'm not here much anymore.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement