• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

ComponentInstance name and Group name

Scheduled Pinned Locked Moved Developers' Forum
21 Posts 4 Posters 719 Views
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.
  • T Offline
    TIG Moderator
    last edited by 26 Apr 2012, 18:30

    Let's put this another way... 😕
    What are you trying to do?
    [In words, not 'code'...]
    I deduce that it's to find if something is an instance and then check that instance.definition.name against a 'text' pattern ??

    If so it is much easier than you are making it...

    TIG

    1 Reply Last reply Reply Quote 0
    • N Offline
      njeremy2
      last edited by 26 Apr 2012, 19:36

      thx ! I'll try this tomorrow

      Hmmm let me explain, it's little bit difficult for me to explain that in English

      I want to create a plugin which count every components and groups in the design.
      With this plugin, you can calculate the texture you need for a group or component (Tomorrow, I will take screenshot to show you my work (but it's in French...)

      For example, I have a component called "Wood Table" (definition name) and you call it "Table 200x300"
      My plugin will count how many tables I have on design and how much texture of Wood I apply on all tables.
      If I don't the plugin calculate the texture (I hope you understand this sentence 😄 ), I have to put "_" (character n° 95 in ASCII TABLE) and the plugin ignore the Table. (ex: "Table 200x300 --> "_Table 200x300")
      But in my case, it only work for the name of groups and component, but I want to add the case for component-name. How should I do ?

      Thanks a lot to everyone ! 😉

      Jeremy

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 26 Apr 2012, 21:44

        Instead of iterating entities to get all instances and then find their definitions, why not iterate the definitions list to get their instances ?

        TIG

        1 Reply Last reply Reply Quote 0
        • N Offline
          njeremy2
          last edited by 26 Apr 2012, 22:18

          @tig said:

          Instead of iterating entities to get all instances and then find their definitions, why not iterate the definitions list to get their instances ?

          I just beginning ruby for sketchup less than 7 days, I don't know anything... 😞

          I just modified the code from someone.
          Tomorrow, I will show you exactly what i'm trying to do.
          I don't have sketchup in that computer

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 26 Apr 2012, 22:33

            I wrote a summary about instances and definitions in SketchUp: http://www.thomthom.net/thoughts/2012/02/definitions-and-instances-in-sketchup/
            Hope it might help.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • N Offline
              njeremy2
              last edited by 27 Apr 2012, 06:47

              @thomthom said:

              I wrote a summary about instances and definitions in SketchUp: http://www.thomthom.net/thoughts/2012/02/definitions-and-instances-in-sketchup/
              Hope it might help.

              I already read it 😉 thanks you !

              1 Reply Last reply Reply Quote 0
              • N Offline
                njeremy2
                last edited by 27 Apr 2012, 07:35

                My plugin :

                http://img850.imageshack.us/img850/4889/99044371.png

                http://img844.imageshack.us/img844/2120/46271262.png

                http://img198.imageshack.us/img198/9208/98217431.png

                http://img59.imageshack.us/img59/1905/86462454.png

                http://img32.imageshack.us/img32/4237/71551261.png

                My group has a name : "GroupWood"
                2 same components : 1 with name "Bench1" and the 2nd without name.
                The plugin will retrive all informations and put that in table in HTML webpage.
                In component section, if the name of component is empty, the plugin retrive definition name. If it has a name, it takes the name.

                If I put "" before the name of the group or name of component, the plugin ignore the texture of them.
                But if I put "
                " before the definition name, it doesn't work. I would like to add this third condition my program.

                Someone can help me ? 😄

                http://img42.imageshack.us/img42/2721/36062394.png

                1 Reply Last reply Reply Quote 0
                • T Offline
                  TIG Moderator
                  last edited by 27 Apr 2012, 09:06

                  You are not making this easy by hiding your code from use and then expecting us to 'fix' it.
                  Here is how I would check some text [refernced as 'str'] to see it it starts with a ''
                  str=~/^_/
                  If there's no match it returns 'nil' if there's a match you get 0.
                  So to test a 'name' against the pattern just use
                  ` if name=~/^
                  /

                  do something

                  endTest the group.name, instance.nameand instance.definition.name` in turn 😕

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • N Offline
                    njeremy2
                    last edited by 27 Apr 2012, 12:16

                    I'm not hiding anything, I just forgot to sent you! ^^

                    
                    =begin
                    author ; njeremy2
                    # This software is provided as an example of using the Ruby interface
                    # to SketchUp.
                    
                    # Permission to use, copy, modify, and distribute this software for 
                    # any purpose and without fee is hereby granted, provided that the above
                    # copyright notice appear in all copies.
                    
                    # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
                    # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                    # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                    =end
                    
                    Sketchup;;require 'sketchup'
                    
                    # Conversion pouces² vers m²
                    def p2m2(superficie)
                    	superficie/1550.0
                    end
                    
                    #Conversion pouces3 vers m3
                    def p2m3(volume)
                    	volume/61023.7440947323
                    end
                    
                    #Ajoute à la classe float une méthode permettant d'arrondir avec 2 chiffres après la virgule
                    class Float
                    	def round2
                    		i=((self*100).round.to_f)/100
                    		return i
                    	end
                    end
                    
                    #Permet d'exporter dans un fichier html le nom et la quantité des Composants, Matières, Groupe (+volume). L'utilisateur peut choisir d'exporter les données qu'il souhaite
                    #L'utilisateur peut choisir d'ignorer certains composants ou groupes (comme les végétaux) en les préfixant d'un _
                    class Export
                    	#Initialisation
                    	def initialize
                    		UI.messagebox "Variables chargés 1"
                    		@model = Sketchup.active_model
                    		@entities = @model.entities
                    		@materials = @model.materials
                    		UI.messagebox "Variables chargés 2"
                    		
                    		
                    		
                    		
                    		
                    		
                    		
                    		@tabGroupes = []
                    		@tabMateriaux = []
                    		@tabComposants = []
                    		@nomFichier = @model.path
                    		@cheminScript= '/ATI/scripts/exportS.html'
                    		4.times do
                    			@nomFichier.chop!
                    		end
                    		@nomFichier += "_ExportS.html"
                    		input()
                    	end
                    	
                    	#Demande de renseignements à l'utilisateur
                    	def input
                    		prompts = ["Nom et chemin du fichier à exporter", 
                    			   "Composants",
                    			   "Matières en m²",
                    			   "Groupes"
                    			   ]
                    			   # "Volume des Groupes" supprimé du formulaire
                    		defs = ["#{@nomFichier}", "Oui", "Oui", "Oui", "Non"] # "non" supprimé pour volume des groupes
                    		list = ["", "Oui|Non", "Oui|Non", "Oui|Non"]		
                    		resultats = inputbox(prompts, defs, list, "Paramétrer l'export")
                    		@nomFichier = resultats[0].to_s
                    		@composants = resultats[1].to_s
                    		@matiere = resultats[2].to_s
                    		@groupe = resultats[3].to_s
                    		# @volume = resultats[4].to_s
                    	end
                    	
                    	#Exporte le nombre et le nom de chaque chose dans un fichier html défini par l'utilisateur
                    	def exporter
                    		listerElements()
                    
                    		#Créer le fichier
                    		fichierExport = File.new("#{@nomFichier.to_s}", "w")
                    		
                    		#Ecrire les informations dedans
                    		#Formulaire html
                    		fichierExport.write "<html>"
                    			fichierExport.write "<form method=\"GET\" action=\""
                    			fichierExport.write @cheminScript.to_s
                    			fichierExport.write "\">"
                    			
                    			#Titre en haut a gauche de l'écran (titre du fichier de sauvegarde)
                    			fichierExport.write "<head>"
                    				fichierExport.write "Projet ; <b>"+(@model.title).to_s+"</b><br />"
                    				fichierExport.write "<input type=\"hidden\" value=\"#{@model.title}\" name=\"nomModele\">"
                    			fichierExport.write "</head>"
                    			
                    			#Tableau
                    			fichierExport.write "<table border=\"1\" align=\"CENTER\">"
                    			
                    			#Affichage des têtes de colonnes
                    			fichierExport.write "<tr><td></td><td bgcolor=\"#99FF00\"><b>Nom</b></td><td bgcolor=\"#99FF00\"><b>Quantité ou surface</b></td></tr>"
                    			
                    			#Composants
                    			fichierExport.write "<tr><td bgcolor=\"#7CF8F8\"><b>Composants</b></td></tr>"
                    			i = 0
                    			@tabComposants.each do |composants|
                    				fichierExport.write "<tr><td></td>"
                    				fichierExport.write "<td>"
                    				if (composants[0].name == "")
                    					fichierExport.write (composants[0].definition.name).to_s
                    					fichierExport.write "<input type=\"hidden\" value=\"#{composants[0].definition.name}\" name=\"nomComposant#{i}\">"
                    					else
                    					fichierExport.write (composants[0].name).to_s
                    					fichierExport.write "<input type=\"hidden\" value=\"#{composants[0].name}\" name=\"nomComposant#{i}\">"
                    				end #endif
                    				fichierExport.write "</td>"
                    				# fichierExport.write "<td>" ## Ajout colonne début
                    				# fichierExport.write (composants[0].definition.name).to_s
                    				# fichierExport.write "<input type=\"hidden\" value=\"#{composants[0].definition.name}\" name=\"nomComposant#{i}\">"
                    				# fichierExport.write "</td>" ## Ajout d'une colonne fin
                    				fichierExport.write "<td>"
                    				fichierExport.write composants[1].to_s
                    				fichierExport.write "<input type=\"hidden\" value=\"#{composants[1].to_s}\" name=\"nbComposant#{i}\">"
                    				fichierExport.write "</td>"
                    				fichierExport.write "</tr>"
                    				i += 1
                    			end			
                    			
                    			#Groupes
                    			fichierExport.write "<tr><td bgcolor=\"#7CF8F8\"><b>Groupes</b></td></tr>"
                    			i = 0
                    			@tabGroupes.each do |groupe|
                    				fichierExport.write "<tr><td></td>"
                    				fichierExport.write "<td>"
                    				fichierExport.write (groupe[0].name).to_s
                    				fichierExport.write "<input type=\"hidden\" value=\"#{(groupe[0].name).to_s}\" name=\"nomGroupe#{i}\">"
                    				fichierExport.write "</td>"
                    				# fichierExport.write "<td>" ## Ajout colonne début
                    				
                    				
                    				
                    				# fichierExport.write "</td>" ## Ajout d'une colonne fin
                    				fichierExport.write "<td>"
                    				fichierExport.write groupe[1].to_s
                    				fichierExport.write "<input type=\"hidden\" value=\"#{groupe[1].to_s}\" name=\"nbGroupe#{i}\">"
                    				fichierExport.write "</td>"
                    				# #Si on gère les volumes et que le groupe n'est pas plat, on affiche son volume
                    				# if @volume == "Oui" && p2m3(groupe[0].volume) > 0
                    					# fichierExport.write "<td>"
                    					# fichierExport.write (p2m3(groupe[0].volume).round2).to_s
                    					# fichierExport.write " m3"
                    					# fichierExport.write "<input type=\"hidden\" value=\"#{(p2m3(groupe[0].volume).round2).to_s}\" name=\"volumeGroupe#{i}\">"
                    					# fichierExport.write "</td>"
                    				# end
                    				fichierExport.write "</tr>"
                    				i += 1
                    			end
                    			
                    			if @matiere == "Oui"
                    				#tout péter
                    				nb = eclater(@entities)
                    				
                    				#Calculer la surface des matériaux
                    				calculerSurfaceMateriau(@entities)
                    				
                    				#On purge le tableau des matériaux qui n'ont pas de surface (cas qui peut arriver s'il y a des végétaux et que l'utilisateur à préfixé le nom
                    				#avec un _
                    				@tabMateriaux = purgerTab(@tabMateriaux)				
                    				
                    				#tout reconstruire
                    				annuler(nb)
                    			end
                    			
                    			#Matières
                    			fichierExport.write "<tr><td bgcolor=\"#7CF8F8\"><b>Matières</b></td></tr>"
                    			i = 0
                    			@tabMateriaux.each do |matiere|
                    				fichierExport.write "<tr><td></td>"
                    				fichierExport.write "<td>"
                    				fichierExport.write (matiere[0].display_name).to_s
                    				fichierExport.write "<input type=\"hidden\" value=\"#{matiere[0].display_name.to_s}\" name=\"nomMat#{i}\">"
                    				fichierExport.write "</td>"
                    				# fichierExport.write "<td>" ## Ajout colonne début
                    				
                    				
                    				
                    				# fichierExport.write "</td>" ## Ajout d'une colonne fin
                    				fichierExport.write "<td>"
                    				fichierExport.write (matiere[1].round2).to_s
                    				fichierExport.write " m²"
                    				fichierExport.write "<input type=\"hidden\" value=\"#{matiere[1].round2.to_s}\" name=\"surfaceMat#{i}\">"
                    				fichierExport.write "</td>"
                    				fichierExport.write "</tr>"
                    				i += 1
                    			end	
                    		
                    			#Nombre de groupes, de composants et de matériau
                    			fichierExport.write "<input type=\"hidden\" value=\"#{@tabGroupes.length}\" name=\"nbGroupes\">"
                    			fichierExport.write "<input type=\"hidden\" value=\"#{@tabComposants.length}\" name=\"nbCompos\">"
                    			fichierExport.write "<input type=\"hidden\" value=\"#{@tabMateriaux.length}\" name=\"nbMats\">"
                    		
                    		#Fermeture des balises et bouton d'export au format csv
                    			fichierExport.write "</table>"
                    			fichierExport.write "<br />"
                    			fichierExport.write "<input type=\"submit\" value=\"Exporter au format csv\">"
                    			fichierExport.write "</form>"
                    		fichierExport.write "</html>"
                    		
                    		#Fermer le fichier
                    		fichierExport.close
                    		
                    		#Message pour l'utilisateur
                    		UI.openURL @nomFichier	
                    		
                    	end	
                    	
                    	#Crée un tableau avec tout les éléments comptés par type
                    	def listerElements	
                    		#On parcours tout les éléments et on les comptes
                    		@entities.each do |groupes|
                    			if groupes.typename == "Group" && @groupe == "Oui"
                    				compter(groupes, 1)
                    			elsif groupes.typename == "ComponentInstance" && @composants == "Oui"
                    				compter(groupes, 2)
                    			end
                    		end
                    		if @matiere == "Oui"
                    			#On purge les matériaux inutilisés
                    			@materials.purge_unused
                    			@materials.each do |materiau|
                    				compter(materiau, 3)
                    			end	
                    		end
                    	end
                    	
                    	#Regarde l'objet passé en paramètre et en tient le compte dans le tableau correspondant
                    	def compter(chose, type)
                    		#Init
                    		trouve = false
                    		
                    		#On récupère le tableau qu'on l'on va devoir traiter
                    		tab = @tabGroupes if type == 1
                    		tab = @tabComposants if type == 2
                    		tab = @tabMateriaux if type == 3
                    		#x représente le nombre d'occurences, si c'est un matériau on l'initialise à 0 car on contera la surface totale plus tard.
                    		#si c'est un groupe ou composant on l'initialise à 1 pour pouvoir compter
                    		if type > 2
                    			x = 0
                    		else
                    			x = 1
                    		end
                    		
                    		
                    		#On regarde si l'élément existe déja, si oui on incrémente la case du tableau qui compte, si non on ajoute une nouvelle case
                    		tab.each do |element|
                    			if chose.name == element[0].name
                    				element[1] += x
                    				trouve = true
                    			end
                    		end
                    		
                    		if trouve == false
                    			tab.push([chose, x])
                    		end
                    		
                    		#on rend le tableau que l'on à traité
                    		@tabGroupes = tab if type == 1
                    		@tabComposants = tab if type == 2
                    		@tabMateriaux = tab if type == 3
                    	end
                    	
                    	#Calcule la surface de chaque matériau
                    	def calculerSurfaceMateriau(entities)
                    		#Récupérer toutes les faces
                    		tabFaces = []
                    		entities.each do |element|
                    			if element.typename == "Face"
                    				tabFaces.push(element)
                    			end
                    		end
                    		
                    		#On regarde si la face possède un matériau, et si oui on calcule la surface
                    		tabFaces.each do |face|
                    			materiau = face.material
                    			materiauBack = face.back_material
                    			if materiau	|| materiauBack			
                    				surface = p2m2(face.area)
                    				@tabMateriaux.each do |materiauxEnregistres|
                    					if materiau
                    						if materiauxEnregistres[0].display_name == materiau.display_name
                    							materiauxEnregistres[1] += surface
                    						end
                    					end
                    					if materiauBack
                    						if materiauxEnregistres[0].display_name == materiauBack.display_name
                    							materiauxEnregistres[1] += surface
                    						end
                    					end
                    				end
                    			end
                    		end
                    	end
                    
                    	#Eclate tout les groupes et composants
                    	def eclater(entities)
                    		nb = 0
                    		composantOuGroupe = true
                    		#Tant qu'il reste un composant ou un groupe, on boucle en permanence pour tous les éclater. Dès qu'il n'y en a plus, on arrête		
                    		while 1
                    			trouve = false
                    			if composantOuGroupe == false
                    				break
                    			end
                    			# UI.messagebox "texte juste avant la boucle"
                    			entities.each do |chose|
                    				if chose.typename == "ComponentInstance" || chose.typename == "Group" 
                    				# UI.messagebox "Texte juste apres qu'il vérifie si c'est un groupe ou si c'est un composant"
                    					if chose.name[0] != 95	#On vérifie que la premiere lettre ne soit pas un _ (on compare les codes ascii), [0] designe la position du caractere a rechercher
                    						chose.explode
                    						nb += 1
                    						trouve = true
                    					end
                    				end
                    			end
                    			if trouve == false
                    				composantOuGroupe = false
                    			end
                    		end
                    		return nb
                    	end
                    	
                    	#Annule nb actions
                    	def annuler(nb)
                    		nb.times do
                    			Sketchup.undo
                    		end
                    	end
                    	
                    	#Recoit un tableau de tableau et le vide des éléments uniques
                    	def purgerTab(tab)
                    		tabReturn = []
                    		tab.each do |element|
                    			if element[1] != 0
                    				tabReturn.push(element)
                    			end
                    		end
                    		return tabReturn
                    	end
                    end
                    
                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 27 Apr 2012, 13:12

                      Something like

                      if (chose.is_a?(Sketchup;;ComponentInstance) && chose.definition.name=~/^_/ ) || (chose.name=~/^_/)
                        chose.explode
                        nb += 1
                        trouve = true
                      end
                      

                      😕

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • N Offline
                        njeremy2
                        last edited by 27 Apr 2012, 14:59

                        it didn"t work ...

                        I will try something else. Another way

                        I practice in how I get the name of definition through a table, and then I will associate that with my code

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          TIG Moderator
                          last edited by 27 Apr 2012, 16:11

                          If you have a reference to the instance you can easily get its definition, from which you can easily get the name of that.
                          I don't see where the difficulty is ?
                          You just need to add some extra tests to your code of instances... in more than one place - my example was how to get the initial name to use...

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • N Offline
                            njeremy2
                            last edited by 27 Apr 2012, 20:36

                            @tig said:

                            If you have a reference to the instance you can easily get its definition, from which you can easily get the name of that.
                            I don't see where the difficulty is ?
                            You just need to add some extra tests to your code of instances... in more than one place - my example was how to get the initial name to use...

                            Yeah but i'm beginning ! so I have to learn what is exactly the difference between componentdefinition and componentinstance, it's hard for me !

                            Every document is written in english, I have to translate some words and then i have to understand each specific vocabulary of sketchup ... (in less than 1 week !)

                            I have to go easily on that !! 😄

                            I will continue monday because I have no tools to continue at home

                            thx TIG! 👍

                            1 Reply Last reply Reply Quote 0
                            • N Offline
                              njeremy2
                              last edited by 30 Apr 2012, 08:36

                              Hi everyone !

                              I have found how to correct my code !! 😄

                              It's was just an error of algorithm.

                              At first time I had this condition :

                              chose.name[0] != 95
                              

                              And I would like to have this :

                              chose.name[0] != 95 || chose.definition.name[0] != 95
                              

                              but it doesn't work ...

                              The solution is :

                              chose.name[0] != 95 && chose.definition.name[0] != 95
                              

                              As I said, I'm beginning !

                              Thanks to all! especially TIG

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

                              Advertisement