mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-12 18:43:26 +00:00
NOTE: From now on Boost is required in order to compile IfcOpenShell
==================================================================== - Use boost::optional for optional IFC arguments - Use boost::uuid for the creation of GlobalIds - New IfcGeomObjects setting to force alignment of TopoDS_Face normal to CCW orientation - Removed static Ifc class, renamed to non-static IfcFile. IfcFile renamed to IfcSpfStream - Introduced Logger class - Introduced EntityBuffer class that keeps track of all writable entities created to add them all in once to the IfcFile class
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/********************************************************************************
|
||||
/********************************************************************************
|
||||
* *
|
||||
* This file is part of IfcOpenShell. *
|
||||
* *
|
||||
@@ -27,19 +27,97 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HAS_BOOST_UUID
|
||||
|
||||
#ifdef HAS_BOOST_UUID
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/uuid_generators.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
#endif
|
||||
|
||||
#include "IfcWrite.h"
|
||||
|
||||
static const char* chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$";
|
||||
|
||||
#ifdef HAS_BOOST_UUID
|
||||
|
||||
// Converts an unsigned integer into a base64 string of length l
|
||||
std::string base64(unsigned v, int l) {
|
||||
std::string r;
|
||||
r.reserve(l);
|
||||
while ( v ) {
|
||||
r.push_back(chars[v%64]);
|
||||
v /= 64;
|
||||
}
|
||||
while ( r.size() != l ) r.push_back('0');
|
||||
std::reverse(r.begin(),r.end());
|
||||
return r;
|
||||
}
|
||||
|
||||
// Converts a base64 string into an unsigned integer
|
||||
unsigned from_base64(const std::string& s) {
|
||||
std::string::size_type zeros = s.find_first_not_of('0');
|
||||
unsigned r = 0;
|
||||
if ( zeros != std::string::npos )
|
||||
for ( std::string::const_iterator i = s.begin()+zeros; i != s.end(); ++ i ) {
|
||||
r *= 64;
|
||||
const char* c = strchr(chars,*i);
|
||||
if ( !c ) throw IfcException("Failed to decode GlobalId");
|
||||
r += (c-chars);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// Compresses the UUID byte array into a base64 representation
|
||||
std::string compress(unsigned char* v) {
|
||||
std::string r;
|
||||
r.reserve(22);
|
||||
r += base64(v[0],2);
|
||||
for ( unsigned i = 1; i < 16; i += 3 ) {
|
||||
r += base64((v[i]<<16) + (v[i+1]<<8) + v[i+2],4);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// Expands the base64 representation into a UUID byte array
|
||||
void expand(const std::string& s, std::vector<unsigned char>& v) {
|
||||
v.push_back(from_base64(s.substr(0,2)));
|
||||
for( unsigned i = 0; i < 5; ++i ) {
|
||||
unsigned d = from_base64(s.substr(2+4*i,4));
|
||||
for ( unsigned j = 0; j < 3; ++ j ) {
|
||||
v.push_back((d>>(8*(2-j))) % 256);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A random number generator for the UUID
|
||||
static boost::uuids::basic_random_generator<boost::mt19937> gen;
|
||||
|
||||
#endif
|
||||
|
||||
IfcWrite::IfcGuidHelper::IfcGuidHelper() {
|
||||
if ( ! seeded ) { srand((unsigned int)time(0)); seeded = true; }
|
||||
#ifdef HAS_BOOST_UUID
|
||||
boost::uuids::uuid u = gen();
|
||||
std::vector<unsigned char> v(u.size());
|
||||
std::copy(u.begin(), u.end(), v.begin());
|
||||
data = compress(&v[0]);
|
||||
|
||||
std::vector<unsigned char> v2;
|
||||
expand(data,v2);
|
||||
boost::uuids::uuid u2;
|
||||
std::copy(v2.begin(), v2.end(), u2.begin());
|
||||
#else
|
||||
if ( ! seeded ) { srand((unsigned int)time(0)); seeded = true; }
|
||||
data.resize(length);
|
||||
for ( unsigned int i = 0; i < length; ++ i ) {
|
||||
data[i] = chars[rand()%strlen(chars)];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
IfcWrite::IfcGuidHelper::operator std::string() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
bool IfcWrite::IfcGuidHelper::seeded = false;
|
||||
bool IfcWrite::IfcGuidHelper::seeded = false;
|
||||
|
||||
Reference in New Issue
Block a user