Hello
I digested the answers a bit.
when i activate extend self at the start of the script it bug i don’t know if i placed it correctly or really what it is for.
I created according to your advice a definition and I use the definition in the command
on another command that create dynamic attributes for a selected object set
in the command I create an array of attributes which are themselves a hash of key value of the meta attributes
Here is the code of my command
###
#Commande N°1 ATTRIBUT RAPPORT GENERIQUE
###
cmd1 = UI::Command.new("Attributs rapport") {
UI.messagebox("Creation des tableauattributs rapport pour le composant selectionné")
# ### DEFINIR LES CENTIMETRES COMME UNITE DE CALCUL DU COMPOSANT
mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|
s.set_attribute "dynamic_attributes","_lengthunits","CENTIMETERS"
$dc_observers.get_latest_class.redraw_with_undo(s)
end
#Initialisation du tableau d'attribut
dc_attributs = []
# Creer un attribut de separation RAPPORT
rapport = {
"aname" => "ra_",
"alabel" => "ra_",
"aformulaunits" => "STRING",
"aaccess" => "VIEW",
"aformlabel" => "***RAPPORT***",
"aoptions" => "&",
"aformula" => "",
"aunits" => "STRING",
"avalue" => "**********"
}
dc_attributs << rapport
# Creer un attribut Epaisseur saisie manuellement
epaisseur = {
"aname" => "ra_epaisseur",
"alabel" => "ra_Epaisseur",
"aformulaunits" => "CENTIMETERS",
"aaccess" => "TEXTBOX",
"aformlabel" => "Epaisseur",
"aoptions" => "&",
"aformula" => "",
"aunits" => "CENTIMETERS",
"avalue" => "1"
}
dc_attributs << epaisseur
# Creer un attribut ARTICLE
article = {
"aname" => "ra_article",
"alabel" => "ra_Article",
"aformulaunits" => "STRING",
"aaccess" => "TEXTBOX",
"aformlabel" => "Article",
"aoptions" => "&",
"aformula" => "",
"aunits" => "STRING",
"avalue" => "DC_DEF_NAME"
}
dc_attributs << article
# Creer un attribut PID
pid = {
"aname" => "ra_pid",
"alabel" => "ra_pid",
"aformulaunits" => "STRING",
"aaccess" => "VIEW",
"aformlabel" => "ID",
"aoptions" => "&",
"aformula" => "",
"aunits" => "STRING",
"avalue" => "DC_DEF_PID"
}
dc_attributs << pid
# Creer un attribut COMMENTAIRE
commentaire = {
"aname" => "ra_commentaire",
"alabel" => "ra_Commentaire",
"aformulaunits" => "STRING",
"aaccess" => "TEXTBOX",
"aformlabel" => "Commentaire",
"aoptions" => "&",
"aformula" => "",
"aunits" => "STRING",
"avalue" => ""
}
dc_attributs << commentaire
# Creer un attribut PRIX UNITAIRE
prix_unitaire = {
"aname" => "ra_prix_unitaire",
"alabel" => "ra_Prix_unitaire",
"aformulaunits" => "FLOAT",
"aaccess" => "TEXTBOX",
"aformlabel" => "Prix unitaire",
"aoptions" => "&",
"aformula" => "",
"aunits" => "EUROS",
"avalue" => "DC_DEF_PRICE"
}
dc_attributs << prix_unitaire
# Creer un attribut QTT saisie manuellement
qtt_saisie = {
"aname" => "ra_qtt_saisie",
"alabel" => "ra_QTT_Saisie",
"aformulaunits" => "FLOAT",
"aaccess" => "TEXTBOX",
"aformlabel" => "Quantite saisie",
"aoptions" => "&",
"aformula" => "",
"aunits" => "FLOAT",
"avalue" => "1"
}
dc_attributs << qtt_saisie
# Creer un attribut Type Quantite
type = {
"aname" => "ra_type",
"alabel" => "ra_Type",
"aformulaunits" => "STRING",
"aaccess" => "LIST",
"aformlabel" => "Type quantité",
"aoptions" => "&U=U&H=H&FT=FT&Longueur%20X=LenX&Longueur%20Y=LenY&Longueur%20Z=LenZ&Surface%20XY=Surface%20XY&Surface%20XZ=Surface%20XZ&Surface%20YZ=Surface%20YZ&Surface%20volumique=Surface%20volumique&Volume%20XYZ=Volume%20XYZ&Volume%20m3=Volume%20m3&Volume%20dm3=Volume%20dm3&Volume%20cm3=Volume%20cm3&Volume%20litres=Volume%20litres&Volume%20dl=Volume%20dl&Volume%20cl=Volume%20cl&Volume%20ml=Volume%20ml&",
"aformula" => "",
"aunits" => "STRING",
"avalue" => "U"
}
dc_attributs << type
# Creer un attribut QTT Calculee
qtt = {
"aname" => "ra_qtt",
"alabel" => "ra_QTT",
"aformulaunits" => "FLOAT",
"aaccess" => "VIEW",
"aformlabel" => "QTT",
"aoptions" => "&",
"aformula" => "CHOOSE(OPTIONINDEX(\"ra_Type\"),ra_QTT_Saisie,ra_QTT_Saisie,ra_QTT_Saisie,ROUND(LenX/100,3),ROUND(LenY/100,3),ROUND(LenZ/100,3),ROUND(Volume()/61023.7440947/(LenZ/100),3),ROUND(Volume()/61023.7440947/(LenY/100),3),ROUND(Volume()/61023.7440947/(LenX/100),3),ROUND(VOLUME()/61023.7440947/(ra_Epaisseur/100),3),ROUND(LenX*LenY*LenZ/1000000,3),Round(VOLUME()/61023.7440947,3),Round(VOLUME()/61023.7440947*1000,3),Round(VOLUME()/61023.7440947*1000000,3),Round(VOLUME()/61023.7440947*1000,3),Round(VOLUME()/61023.7440947*10000,3),Round(VOLUME()/61023.7440947*100000,3),Round(VOLUME()/61023.7440947*1000000,3))",
"aunits" => "FLOAT",
"avalue" => "1"
}
dc_attributs << qtt
# Creer un attribut Unite Calculee
unite = {
"aname" => "ra_unite",
"alabel" => "ra_Unite",
"aformulaunits" => "STRING",
"aaccess" => "VIEW",
"aformlabel" => "Unite",
"aoptions" => "&",
"aformula" => "CHOOSE(OPTIONINDEX(\"ra_Type\"),\"u\",\"h\",\"ft\",\"m\",\"m\",\"m\",\"m²\",\"m²\",\"m²\",\"m²\",\"m³\",\"m³\",\"dm³\",\"cm³\",\"l\",\"dl\",\"cl\",\"ml\")",
"aunits" => "STRING",
"avalue" => "u"
}
dc_attributs << unite
# Creer un attribut SOUS TOTAL
sous_total = {
"aname" => "ra_sous_total",
"alabel" => "ra_Sous_Total",
"aformulaunits" => "FLOAT",
"aaccess" => "VIEW",
"aformlabel" => "Sous total",
"aoptions" => "&",
"aformula" => "ra_QTT*ra_Prix_Unitaire",
"aunits" => "EUROS",
"avalue" => "0"
}
dc_attributs << sous_total
creer_attribut_dynamique(dc_attributs)
}
###FIN COMMANDE N°1
In my method definition I don’t know how to retrieve the array passed as parameters in the command
I tried with and without “# {array}”
here is the method code
# First we pull in the standard API hooks.
require 'sketchup.rb'
DCDICT = "dynamic_attributes"
#extend self
# Show the Ruby Console at startup so we can
# see any programming errors we may make.
Sketchup.send_action "showRubyPanel:"
#_____________________________DEFINITIONS METHODES__________________________________________
#Définition de la méthode pour créer un attribut dynamique sur une selection
def creer_attribut_dynamique(dc_attributs)
# aname => nom de l'attribut sans espace, sans majuscule, sans "_" ou nombre en premier caractère valeur par défaut "attribut"
# alabel => label de l'attribut sans espace, sans "_" ou nombre en premier caractère valeur par défaut "Attribut"
# aformulaunits => unité de l'attribut "STRING"|"FLOAT"|"CENTIMETERS"|"INCHES" par défaut "STRING"
# aaccess => access mode de visibilité dans le panneau option "TEXTBOX"|"NONE"|"VIEW"|"LIST" par défaut "TEXTBOX"
# aformlabel => etiquette de l'attribut dans le panneau option par défaut "Attribut"
# aoptions => liste d'option déroulante si access = "LISTE" . liste de CLE=VALEUR encadrée par de "&" exemple "&CLE1=VALEUR1&CLE2=VALEUR2&" valeur par défaut "&"
# aformula => Formule ne pas préfixer ave "=", si aucun laissé vide, valeur par défaut ""
# aunits => unité de l'attribut dans le formulaire "STRING|FLOAT|PERCENT|INTEGER|BOOLEAN|CENTIMETERS|MILLIMETERS|METERS|INCHES|FEET|YARDS|DEGRES|DOLLARS|EUROS|YEN|POUNDS|KILOGRAMS" par défaut "STRING"
# avalue => valeur de l'attribut
model = Sketchup.active_model
#On indique le début de l'opération pour une éventuelle annulation
model.start_operation('créer attribut', true,true,false)
model.selection.grep(Sketchup::ComponentInstance) { |entity|
# This will check if there is an dictionary with the given name. The second
# argument tell SU to not create it if it's missing.
#next unless entity.definition.attribute_dictionary('dynamic_attributes', false)
# Now we can be sure the component has the DC dictionary.
# NOTE: When you do this you end up creating multiple undo operations.
# If you plan on releasing this you should make sure you wrap the
# code up on model.start/commit_operation such that everything can be
# undone in one step. (Requirement for Extension Warehouse)
#on récupère la définition de l'instance, toutes les instances du composants sélectionnées ou pas recevront la création d'attribut
sd = entity.definition
# Iteration sur chaque valeur du tableau dc_attributs
dc_attributs.each do |dc_attribut|
# #on definit les avriables pour charque tableauattributs
aname = dc_attribut["aname"]
alabel = dc_attribut["alabel"]
aformulaunits = dc_attribut["aformulaunits"]
aaccess = dc_attribut["aaccess"]
aformlabel = dc_attribut["aformlabel"]
aoptions = dc_attribut["aoptions"]
aformula = dc_attribut["aformula"]
aunits = dc_attribut["aunits"]
avalue = dc_attribut["avalue"]
#on test la valeur de avalue passer en parametre pour verifier quelle ne contient pas une commande spéciale
#commande spéciales :
# DC_DEF_NAME on récupère le nom du composant
# DC_DEF_PRICE le prix
# DC_DEF_PID l'ID persistant
if avalue == "DC_DEF_NAME"
avalue = sd.name
elsif avalue == "DC_DEF_PRICE"
avalue = sd.get_attribute("SU_DefinitionSet", "Price", 0)
elsif avalue == "DC_DEF_PID"
avalue = entity.persistent_id
end
#Définition des noms des méta-tableauattributs
anameaccess = "_" + aname + "_access"
anameformlabel = "_" + aname + "_formlabel"
anameformulaunits = "_" + aname + "_formulaunits"
anamelabel = "_" + aname + "_label"
anameoptions = "_" + aname + "_options"
anameunits = "_" + aname + "_units"
anameformula = "_" + aname + "_formula"
#On définit l'attribut et les méta-tableauattributs
sd.set_attribute(DCDICT, anameaccess, aaccess)
sd.set_attribute(DCDICT, anameformlabel, aformlabel)
sd.set_attribute(DCDICT, anameformulaunits, aformulaunits)
sd.set_attribute(DCDICT, anamelabel, alabel)
sd.set_attribute(DCDICT, anameoptions, aoptions)
sd.set_attribute(DCDICT, anameunits, aunits)
# en cas d'abscence de formule on ne créer pas le meta-attribut formula
if aformula != ""
sd.set_attribute(DCDICT, anameformula, aformula)
end
sd.set_attribute(DCDICT, aname, avalue)
#on incremente i
i += 1
end #Fin de la boucle sur chaque entite du tableau tableauattributs
#On redessine le composant
dcs = $dc_observers.get_latest_class
dcs.redraw_with_undo(entity)
}
status = model.commit_operation
if status == true
UI.messagebox('Attribut(s) créé(s)',MB_OK)
end
end
#Fin définition méthode creer_attribut_dynamique
thank you