ComponentInstance name and Group name
-
I'm a beginner, it's my first week in Ruby developement
I've just finish to read "Automatic_Sketchup.pdf" from http://www.autosketchup.com/ and it's really good !
chose.typename == "ComponentInstance" chose.typename == "Group"
The entire code already work, but I just want to add a third condition.
I don't really know how to use it.
if chose.typename == "ComponentInstance" || chose.typename == "Group" || [b]groupdefn[/b]
-
this is my code :
#Explode all components and groups def Explosion(entities) nb = 0 componentOrGroup = true #Stop until there is no group ou component left while 1 found = false if componentOrGroup == false break end entities.each do |chose| if chose.typename == "ComponentInstance" || chose.typename == "Group" || ???? if chose.name[0] != 95 #We check if the first character is "_" from ASCII table, if it's right we explode the group or component chose.explode nb += 1 found = true end end end if found == false componentOrGroup = false end end return nb end
-
Do not use
typename
, is VERY slow.Use
.is_a?()
ie:
if chose.is_a?(Sketchup::ComponentInstance) || chose.is_a?(Sketchup::Group)
-
Yes - as Dan says never ever use
.typename
. -
There is NO
|| ????
You could try alternative tests: e.g. if it's an instance get theinstance.definition.name
etcAlso methods [def] should start with a lower-case letter...
-
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...
-
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
-
Instead of iterating entities to get all instances and then find their definitions, why not iterate the definitions list to get their instances ?
-
@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 -
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. -
@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 !
-
My plugin :
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 ?
-
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
end
Test the
group.name,
instance.nameand
instance.definition.name` in turn -
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
-
Something like
if (chose.is_a?(Sketchup;;ComponentInstance) && chose.definition.name=~/^_/ ) || (chose.name=~/^_/) chose.explode nb += 1 trouve = true end
-
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
-
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 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!
-
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
Advertisement