Color values to skm code

Continuing the discussion from How to import 4000+ colors:

@lori
I’ll post the html to skm code here so others can offer advice or alternatives…

<?php

//include simple html dom...
require('simple_html_dom.php');

//get page source...
$html = file_get_html('file://localhost/Users/johns_iMac/tmp/16_alpha/skm_color_template/pantone_coated_table.html');

$count = 0;

$main_folder = "/private/tmp/Pantone Coated";

if (!file_exists($main_folder)) {
   mkdir($main_folder, 0777);
}


//loop through each <tr>...
foreach($html->find('tr') as $row) {

	//extract values from each <td>...
	$name 	= $row->find('td', 0)->innertext;
	$hex 	= $row->find('td', 1)->innertext;
	$red 	= $row->find('td', 2)->innertext;
	$green 	= $row->find('td', 3)->innertext;
	$blue 	= $row->find('td', 4)->innertext;
	//ignore <table> 'header' row...
	if($count > 0) {

$save_folder = $main_folder.$name;
if (!file_exists($save_folder)) {
   mkdir($save_folder, 0777);
};

if (file_exists($save_folder)){
$path = $save_folder."/document.xml";
 };

$mydocument = fopen($path, "w") or die("Unable to open file!");
$document = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<materialDocument xmlns="http://sketchup.google.com/schemas/sketchup/1.0/material" xmlns:mat="http://sketchup.google.com/schemas/sketchup/1.0/material" xmlns:r="http://sketchup.google.com/schemas/1.0/references" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/sketchup/1.0/material http://sketchup.google.com/schemas/sketchup/1.0/material.xsd">

  <mat:material colorBlue="
XML
.$blue.
<<<XML
" colorGreen="
XML
.$green.
<<<XML
" colorRed="
XML
.$red.
<<<XML
" colorizeType="0" hasTexture="0" name="[
XML
.$name.
<<<XML
]" trans="0" type="0" useTrans="0"/>

</materialDocument>
XML;

fwrite($mydocument, $document);
fclose($mydocument);

$path1 =  $save_folder."/references.xml";
$myrefs = fopen($path1, "w") or die("Unable to open file!");
$refs = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<references xmlns="http://sketchup.google.com/schemas/1.0/references" xmlns:r="http://sketchup.google.com/schemas/1.0/references" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/1.0/references http://sketchup.google.com/schemas/1.0/references.xsd"/>

XML;

fwrite($myrefs, $refs);
fclose($myrefs);

$path2 = $save_folder."/documentProperties.xml";
$mydocumentProperties = fopen($path2, "w");
date_default_timezone_set('UTC');
$today = date("Y-m-d\TH:i:s");
$documentProperties = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<documentProperties xmlns="http://sketchup.google.com/schemas/1.0/documentproperties" xmlns:dp="http://sketchup.google.com/schemas/1.0/documentproperties" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/1.0/documentproperties http://sketchup.google.com/schemas/1.0/documentproperties.xsd">

  <dp:title>[
XML
.$name.
<<<XML
]</dp:title>

  <dp:description></dp:description>

  <dp:creator>jcb</dp:creator>

  <dp:keywords></dp:keywords>

  <dp:lastModifiedBy></dp:lastModifiedBy>

  <dp:revision>0</dp:revision>

  <dp:created>
XML
.$today.
<<<XML
</dp:created>

  <dp:modified>
XML
.$today.
<<<XML
</dp:modified>

  <dp:thumbnail>doc_thumbnail.png</dp:thumbnail>

  <dp:generator dp:name="Material" dp:version="1"/>

</documentProperties>
XML;

fwrite($mydocumentProperties, $documentProperties);
fclose($mydocumentProperties);


$width = "64";
$height = "64";
$image	= imagecreate( $width, $height );
$main_color	= imagecolorallocate( $image, $red, $green, $blue );

imagefill( $image, 0, 0, $main_color);

$save = $save_folder."doc_thumbnail.png";
chmod($save,0755);
imagepng($image, $save, 0, NULL);
imagedestroy($image);

	$count++;

}}

you’ll need to have php installed and can probably find a csv module to do the same as simple html does in this one…

john

1 Like

@lori,

if we continue here maybe @jim_foltz or @TIG can help out with the csv list extraction…

maybe even a file read would work…

can you post a small portion of the csv file, i.e. 10 colors…

john

@john_drivenupthewall,

That would be amazing. Sure, I can post that. Do I post the lines on this thread or attach the sample .csv?

either, if you post inline you add it in a code block using this method

john

How’s this? color-samples.zip (439 Bytes)

[only works if you are importing a handful of colors - see @john_drivenupthewall’s comment below]

If you paste the following code into the Ruby Console and hit enter, it should import colors from any CSV file in the format you’ve specified in your sample:

model = Sketchup.active_model
materials = model.materials
base = UI.openpanel("Select CSV File", "~", "CSV Files|*.csv;||")
input = File.open(base,'r')
line = input.gets
while (line = input.gets)
  values = line.split(',')          
  material = materials.add(values[1].rstrip)
  material.color = [values[2].to_i,values[3].to_i,values[4].to_i]
end
input.close

The new materials should appear in the Materials pane in the “In Model” section.

@jimhami42,

that only save’s them to the model, and at around about 1200 you will start to run out of memory and slow right down…

you then also need to Save it as a Library manually, which for 4000 colors will definitely break something…

you can’t actually do a Material Library save on mac SU, so it would be one at a time for me…

using php outside of SU, 1400 Pantone colors takes me about 2 minutes…

john

1 Like

Excellent point. After I posted that, I did a sample comparison on model sizes and realized that an empty model with 4,000 colors as I input them wasn’t going to produce a very happy result. The only wrinkle I came across in creating the SKM files is zipping the XML and PNG files together … I didn’t see where you did that in your PHP code. Ruby has ZIP support, but it’s not native to the version I’m using :frowning:

@jimhami42 @john_drivenupthewall -

I gave the Ruby console script a shot and was able to get one of the groups of 1600 colors into the .skp and save it out as a collection. It hung up quite a bit but it worked.

Looks like the .php code is the way to go for the master library of 4000. My .php is really rusty though so I’m struggling to edit what John posted.

I really can’t thank you guys enough for all the help! I’m miles closer than where I was with this project yesterday :smiley:

no, I actually wrote a throw away ruby, run in SU that called the php and then zipped the results using another system call to zip, it would have been something like I use for making rbz files…

           zipity_do = 'zip -vrX "' + rbz_new + '" "' + rbz_file + '" "' + rbz_folder\
           + '" -x "*dev*" -x "*.DS_Store" 2>&1> /tmp/zipity_do.txt' # for debuging
           ` cd "#{rbz_dir}" && #{zipity_do}  `

@lori my php is almost zero, I found a scrapper script for making color swatches removed all the ‘server’ bits and cobbled the local file writes into it…

php does have built in methods str_getcsv and fgetcsv but I can’t quite work it out how to patch that in…

john

I don’t have a platform handy to test this on, but something like this should work to read the CSV file:

[edited to replace previous code with tested example)

$myfile = fopen("pantone.csv", "r");
while($buffer = fgets($myfile)) {
$values = explode(',',$buffer);
$color_no = rtrim($values[0]);
$name = rtrim($values[1]);
$red = rtrim($values[2]);
$green = rtrim($values[3]);
$blue = rtrim($values[4]);
$hex = rtrim($values[5]);
echo $name . " - ";
echo $red. " - ";
echo $green . " - ";
echo $blue . " - ";
echo $hex . "\n";
//  [...]
}
fclose($myfile);

Produces:

STONE BROWN - 184 - 154 - 134 - B89A86
PEANUT BUTTER - 202 - 162 - 127 - CAA27F
BURNT ALMOND - 182 - 150 - 122 - B6967A
CLASSIC TAUPE - 213 - 189 - 167 - D5BDA7
RIVIERA SAND - 224 - 198 - 163 - E0C6A3
BROWN TEEPEE - 189 - 167 - 148 - BDA794
TOFFEE CRUNCH - 160 - 136 - 114 - A08872
RAFFIA CREAM - 237 - 223 - 202 - EDDFCA
GOBI DESERT - 206 - 188 - 165 - CEBCA5
HARVEST BROWN - 186 - 166 - 140 - BAA68C
RHINO - 190 - 195 - 188 - BEC3BC

@jimhami42, I missed your post, but got this to work on mac…

#!php
<?php
//get page source...
$csv = file_get_contents("/Users/johns_iMac/tmp/16_alpha/color-samples.csv") or die("Unable to open file!");
//echo $csv;

$count = 0;

$main_folder = "/private/tmp/csv_skm";

if (!file_exists($main_folder)) {
   mkdir($main_folder, 0777);
}

//loop through each row... 250F-4 ,STONE BROWN ,184,154,134,B89A86
$data = explode("\n", rtrim($csv)); //parse the rows

foreach($data as $line) {

  $row = explode(',', $line);
	//extract values from each <td>...
	$name 	= rtrim($row[1]);
	$hex 	= rtrim($row[5]);
	$red 	= rtrim($row[2]);
	$green 	= rtrim($row[3]);
	$blue 	= rtrim($row[4]);

	$count++;
	//ignore <table> 'header' row...
	if($count > 1) {

	echo $name ;
	$sep = '/';
$save_folder = $main_folder.$sep.$name;
if (!file_exists($save_folder)) {
   mkdir($save_folder, 0777);
};

if (file_exists($save_folder)){
$path = $save_folder."/document.xml";
 };

$mydocument = fopen($path, "w") or die("Unable to open file!");
$document = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<materialDocument xmlns="http://sketchup.google.com/schemas/sketchup/1.0/material" xmlns:mat="http://sketchup.google.com/schemas/sketchup/1.0/material" xmlns:r="http://sketchup.google.com/schemas/1.0/references" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/sketchup/1.0/material http://sketchup.google.com/schemas/sketchup/1.0/material.xsd">

  <mat:material colorBlue="
XML
.$blue.
<<<XML
" colorGreen="
XML
.$green.
<<<XML
" colorRed="
XML
.$red.
<<<XML
" colorizeType="0" hasTexture="0" name="[
XML
.$name.
<<<XML
]" trans="0" type="0" useTrans="0"/>

</materialDocument>
XML;

fwrite($mydocument, $document);
fclose($mydocument);

$path1 =  $save_folder."/references.xml";
$myrefs = fopen($path1, "w") or die("Unable to open file!");
$refs = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<references xmlns="http://sketchup.google.com/schemas/1.0/references" xmlns:r="http://sketchup.google.com/schemas/1.0/references" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/1.0/references http://sketchup.google.com/schemas/1.0/references.xsd"/>

XML;

fwrite($myrefs, $refs);
fclose($myrefs);

$path2 = $save_folder."/documentProperties.xml";
$mydocumentProperties = fopen($path2, "w");
date_default_timezone_set('UTC');
$today = date("Y-m-d\TH:i:s");
$documentProperties = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<documentProperties xmlns="http://sketchup.google.com/schemas/1.0/documentproperties" xmlns:dp="http://sketchup.google.com/schemas/1.0/documentproperties" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sketchup.google.com/schemas/1.0/documentproperties http://sketchup.google.com/schemas/1.0/documentproperties.xsd">

  <dp:title>[
XML
.$name.
<<<XML
]</dp:title>

  <dp:description></dp:description>

  <dp:creator>csv2skm</dp:creator>

  <dp:keywords></dp:keywords>

  <dp:lastModifiedBy></dp:lastModifiedBy>

  <dp:revision>0</dp:revision>

  <dp:created>
XML
.$today.
<<<XML
</dp:created>

  <dp:modified>
XML
.$today.
<<<XML
</dp:modified>

  <dp:thumbnail>doc_thumbnail.png</dp:thumbnail>

  <dp:generator dp:name="Material" dp:version="1"/>

</documentProperties>
XML;

fwrite($mydocumentProperties, $documentProperties);
fclose($mydocumentProperties);


$width = "64";
$height = "64";
$image	= imagecreate( $width, $height );
$main_color	= imagecolorallocate( $image, $red, $green, $blue );

imagefill( $image, 0, 0, $main_color);

$save = $save_folder."/doc_thumbnail.png";
imagepng($image, $save, 0, NULL);
imagedestroy($image);


}}

@lori, you will need to replace my hard coded paths and work out any differences in your php version…

mac ships PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45) with El Capitan…

1 Like

Wow again! This is perfect. Let me give it a go and see what happens. You ROCK!!

have a look at php ZipArchive and ZipArchive::addFile and you can probably do it in a single step…

my brain hurts…

@lori, been out all day, but just got the zip and remove to work in php…

add from line 140, before final }} braces…

$skm = $save_folder.'.skm';
echo '  '.$skm;
$zip = new ZipArchive();
$res = $zip->open($skm, ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile($path, 'document.xml');
    $zip->addFile($path1, 'references.xml');
    $zip->addFile($path2, 'documentProperties.xml');
    $zip->addFile($save, 'doc_thumbnail.png');
  $zip->close();
    echo '  ok  ';
} else {
    echo '  failed  ';
}

if (PHP_OS === 'Windows')
{
    exec("rd /s /q {$save_folder}");
}
else // mac + account for spaces in names
{
    exec("rm -R \"{$save_folder}\"");
}

and here they are in SU…

john

2 Likes

Very nice work, John :slight_smile:

1 Like

Thanks so much John! Giving it a try right now.

what does tis return from ‘Ruby Console’ on Windows…

`C:\>where php.exe`

does it return a path?

if so a wrapper may make the whole process easier…

I’m not sure PHP is available on Windows workstations. I run it on most of my Linux servers, although I do have a “WAMP” server running PHP on Windows 2012R2. My Windows 7 Home returns this:

INFO: Could not find files for the given pattern(s).

PHP needs installing on a Windows computer: