Hello
I continue on my way, and i create a new command to export the attributes i generated to a csv file, for use with excel.
If the selection is empty, it searches for all instances, even nested in groups or components.
My first question :
I can’t do the same thing just for the selection it only takes the first level and won’t dig into the nested lower levels.
My second question
At the end I can’t find the correct syntax for declaring that the first line of the csv is a header. headers: true
Float values are written with. in decimal sepator and excel reads them as text! How do I properly declare that the unit, unit price and subtotal columns are decimals?
And a last one on this point how to declare that the file is written in UTF8?
Thanks in advance. Good week
Here is the code
#**************************************************************
#Commande RAPPORT ATTRIBUT CSV
def export_csv_cmd
model = Sketchup.active_model
# #On indique le début de l'opération pour une éventuelle annulation
model.start_operation('créer rapport attributs', true,true,false)
selset = Sketchup::active_model.selection
#si la selection est vide ?
if selset.empty?
# appel de la methode collect_rapport_attribut sur l'ensemble du modèle
collect_rapport_attribut(model.definitions)
else
# Sinon appel de la methode collect_rapport_attribut sur la secelction seulement
collect_rapport_attribut(selset)
end
end
def filtrage_selection (collection=nil)
# fitrage de la selection selon la collection passée en référence
#Si la collection est vide ( aucune séléction)
if collection.nil?
#alors toutes les définitions du modèle
dlist = Sketchup::active_model.definitions
#on rejette les images (le ! indique sur la collection elle même)
dlist.reject! {|d| d.image? }
# sinon si la collection n'est pas vide alors le contenu de la collection auquel on retire les images
elsif collection.all? {|o| o.is_a?(Sketchup::ComponentDefinition) }
dlist = collection.reject {|d| d.image? }
else
insts = collection.grep(Sketchup::ComponentInstance)
groups = collection.grep(Sketchup::Group)
#dlist = insts.map{|i| i.definition } + groups#.map{|g| g.definition }
dlist = insts.map{|i| i.definition } + groups.map{|g| g.definition }
dlist.uniq!
end
dcs = dlist.select {|d| d.attribute_dictionary(DCDICT,false) }
# selection uniquement des composant possédant l'attribut dynamique "ra_" dans le dictionnaire dynamic_attribut
dcrapport = dcs.select {|d| d.get_attribute(DCDICT,"ra_article",false) }
end
def collect_rapport_attribut(collection)
dcrapport = filtrage_selection (collection)
# 1. on creer le tableau avec les valeurs d'entete,
rapporta = [["ID", "Poste","Article","QTT", "Unité", "Prix unitaire", "Sous total", "Fournisseur"]]
i = 1
# 2. Iterate the report component definitions
# get the data from dictionary attributes.
dcrapport.each {|d|
insts = d.instances
insts.each {|inst|
inst_d = inst.definition
id = inst.guid
unless inst.get_attribute(DCDICT,"ra_poste",false)
poste = inst_d.get_attribute(DCDICT,"ra_poste","")
else
poste = inst.get_attribute(DCDICT,"ra_poste","")
end
unless inst.get_attribute(DCDICT,"ra_article",false)
article = inst_d.get_attribute(DCDICT,"ra_article","")
else
article = inst.get_attribute(DCDICT,"ra_article","")
end
unless inst.get_attribute(DCDICT,"ra_qtt",false)
qtt = inst_d.get_attribute(DCDICT,"ra_qtt",0.0)
else
qtt = inst.get_attribute(DCDICT,"ra_qtt",0.0)
end
unless inst.get_attribute(DCDICT,"ra_unite",false)
unite = inst_d.get_attribute(DCDICT,"ra_unite","")
else
fournisseur = inst.get_attribute(DCDICT,"ra_unite","")
end
unless inst.get_attribute(DCDICT,"ra_prix_unitaire",false)
prix_unitaire = inst_d.get_attribute(DCDICT,"ra_prix_unitaire",0.0)
else
prix_unitaire = inst.get_attribute(DCDICT,"ra_prix_unitaire",0.0)
end
unless inst.get_attribute(DCDICT,"ra_sous_total",false)
sous_total = inst_d.get_attribute(DCDICT,"ra_sous_total",0.0)
else
sous_total = inst.get_attribute(DCDICT,"ra_sous_total",0.0)
end
unless inst.get_attribute(DCDICT,"ra_fournisseur",false)
fournisseur = inst_d.get_attribute(DCDICT,"ra_fournisseur","")
else
fournisseur = inst.get_attribute(DCDICT,"ra_fournisseur","")
end
# rapporta[i] = {
# "ID" =>id,
# "Poste" => poste,
# "Article" => article,
# "QTT" => qtt.to_f,
# "Unité" => unite,
# "Prix_unitaire" => prix_unitaire.to_f,
# "Sous total" => sous_total.to_f,
# "Fournisseur" => fournisseur
# }
rapporta[i] = [id, poste, article, qtt.to_f, unite, prix_unitaire.to_f, sous_total.to_f, fournisseur]
i += 1
}
}
mod = Sketchup.active_model
#On creer une variable URL qui a pour valeur le chermin le fichier.skp rechercher dans le dossier d'instalation du plugin
url = Sketchup.find_support_file("test.csv", "Plugins/sj_attribut_rapport/csv/")
i = i-1
j = 0
# CSV.generate(headers: true) ??? UTF8 ??? decimale value ???
CSV.open(url, "wb"). do |csv|
while j < i
csv << rapporta[j]
j +=1
end
end
model = Sketchup.active_model
status = model.commit_operation
# if status == true
# UI.messagebox(rapporta.map{|r| print "#{r}\n"},MB_OK)
# end
end