Skip to content

Commit ab59173

Browse files
committed
Add a stream2_from_string() (mostly for wasm)
1 parent b6abb24 commit ab59173

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/ifcopenshell-python/ifcopenshell/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,33 @@ def stream2(path: Union[Path, str]):
301301
as a dictionary.
302302
303303
Args:
304-
path (Union[Path, str]): _description_
304+
path (Union[Path, str]): input file path
305305
306306
Yields:
307-
_type_: _description_
307+
dict: entity instance dictionaries
308308
"""
309309
streamer = ifcopenshell_wrapper.InstanceStreamer(str(path))
310310
while streamer:
311311
if inst := streamer.read_instance_py():
312312
yield inst
313313

314314

315+
def stream2_from_string(data: str):
316+
"""Streams the content of a file path from string, yielding each instance
317+
as a dictionary.
318+
319+
Args:
320+
data (str): input data
321+
322+
Yields:
323+
dict: entity instance dictionaries
324+
"""
325+
streamer = ifcopenshell_wrapper.stream_from_string(data)
326+
while streamer:
327+
if inst := streamer.read_instance_py():
328+
yield inst
329+
330+
315331
def convert_path_to_rocksdb(ifcspf_path: Union[Path, str], rocksdb_path: Union[Path, str]):
316332
"""Converts an IFC-SPF file on disk to the IfcOpenShell-specific
317333
RocksDB encoding. RocksDB is an embedded key-value store that allows

src/ifcparse/IfcFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class InstanceStreamer {
130130

131131
InstanceStreamer(const std::string& fn);
132132

133+
InstanceStreamer(void* data, int length);
134+
133135
InstanceStreamer(const IfcParse::schema_definition* schema, IfcParse::IfcSpfLexer* lexer);
134136

135137
~InstanceStreamer() {

src/ifcparse/IfcParse.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,33 @@ IfcParse::InstanceStreamer::InstanceStreamer(const std::string& fn)
14991499
}
15001500
}
15011501

1502+
IfcParse::InstanceStreamer::InstanceStreamer(void* data, int length)
1503+
: stream_(new IfcSpfStream(data, length))
1504+
, lexer_(new IfcSpfLexer(stream_))
1505+
, token_stream_(3, Token{})
1506+
, schema_(nullptr)
1507+
, ifcroot_type_(nullptr)
1508+
, progress_(0)
1509+
{
1510+
init_locale();
1511+
1512+
good_ = file_open_status::NO_HEADER;
1513+
if (*stream_) {
1514+
header_ = new IfcParse::IfcSpfHeader(lexer_);
1515+
if (header_->tryRead() && header_->file_schema()->schema_identifiers().size() == 1) {
1516+
try {
1517+
schema_ = IfcParse::schema_by_name(header_->file_schema()->schema_identifiers().front());
1518+
good_ = file_open_status::SUCCESS;
1519+
} catch (const IfcParse::IfcException&) {
1520+
}
1521+
}
1522+
storage_.file = nullptr;
1523+
storage_.schema = schema_;
1524+
storage_.tokens = lexer_;
1525+
storage_.references_to_resolve = &references_to_resolve_;
1526+
}
1527+
}
1528+
15021529
IfcParse::InstanceStreamer::InstanceStreamer(const IfcParse::schema_definition* schema, IfcParse::IfcSpfLexer* lexer)
15031530
: stream_(nullptr)
15041531
, lexer_(lexer)

src/ifcwrap/IfcParseWrapper.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ private:
708708
%newobject open;
709709
%newobject read;
710710
%newobject parse_ifcxml;
711+
%newobject stream_from_string;
711712

712713
%inline %{
713714
IfcParse::IfcFile* open(const std::string& fn, bool readonly=false) {
@@ -728,6 +729,12 @@ private:
728729
return f;
729730
}
730731

732+
IfcParse::InstanceStreamer* stream_from_string(const std::string& data) {
733+
char* copiedData = new char[data.length()];
734+
memcpy(copiedData, data.c_str(), data.length());
735+
return new IfcParse::InstanceStreamer((void *)copiedData, data.length());
736+
}
737+
731738
const char* version() {
732739
return IFCOPENSHELL_VERSION;
733740
}

0 commit comments

Comments
 (0)