OCCT-Light 0.1
C ABI and C++ veneer for multi-language CAD workflows
Loading...
Searching...
No Matches
de.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Capgemini Engineering Research and Development.
2//
3// This file is part of OCCT-Light software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Affero General Public License version 3 as published
7// by the Free Software Foundation, with an option to use any later version.
8// Consult the file LICENSE_AGPL_30.txt included in OCCT-Light distribution
9// for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of a commercial
12// license or contractual agreement.
13//
14// SPDX-License-Identifier: AGPL-3.0-or-later
15
21#ifndef OCCTL_HPP_DE_HPP
22#define OCCTL_HPP_DE_HPP
23
24#include <occtl/occtl_de.h>
25
26#include <occtl-hpp/core.hpp>
27#include <occtl-hpp/topo.hpp>
28
29#include <cstddef>
30#include <cstdint>
31#include <optional>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace occtl::de
37{
38
40{
41 std::string id;
42 std::string label;
43 std::vector<std::string> extensions;
44 bool can_read_file = false;
45 bool can_write_file = false;
46 bool can_read_memory = false;
47 bool can_write_memory = false;
48};
49
52inline std::pair<Graph, NodeId> read(const std::string& thePath)
53{
54 ::occtl_graph_t* aRaw = nullptr;
55 ::occtl_node_id_t aRoot{};
56 check(::occtl_de_read(thePath.c_str(), &aRaw, &aRoot));
57 return {Graph(aRaw), NodeId(aRoot)};
58}
59
62inline std::pair<Graph, NodeId> read_memory(const std::string& theFormatId,
63 const uint8_t* const theData,
64 const std::size_t theSize)
65{
66 ::occtl_graph_t* aRaw = nullptr;
67 ::occtl_node_id_t aRoot{};
68 check(::occtl_de_read_memory(theFormatId.c_str(), theData, theSize, &aRaw, &aRoot));
69 return {Graph(aRaw), NodeId(aRoot)};
70}
71
74inline std::pair<Graph, NodeId> read_memory(const std::string& theFormatId,
75 const std::vector<uint8_t>& theData)
76{
77 return read_memory(theFormatId, theData.empty() ? nullptr : theData.data(), theData.size());
78}
79
81inline void write(const Graph& theGraph, const NodeId theRoot, const std::string& thePath)
82{
83 check(::occtl_de_write(theGraph.get(), theRoot.get(), thePath.c_str()));
84}
85
88inline std::vector<uint8_t> write_memory(const Graph& theGraph,
89 const NodeId theRoot,
90 const std::string& theFormatId)
91{
92 std::size_t aSize = 0;
94 theRoot.get(),
95 theFormatId.c_str(),
96 nullptr,
97 0,
98 &aSize));
99
100 std::vector<uint8_t> aData(aSize);
102 theRoot.get(),
103 theFormatId.c_str(),
104 aData.empty() ? nullptr : aData.data(),
105 aData.size(),
106 &aSize));
107 return aData;
108}
109
111inline std::vector<std::string> supported_formats()
112{
113 size_t aCount = 0;
114 check(::occtl_de_format_ids(nullptr, 0, &aCount));
115 std::vector<const char*> aIds(aCount);
116 size_t aGot = 0;
117 check(::occtl_de_format_ids(aIds.data(), aCount, &aGot));
118 std::vector<std::string> aOut;
119 aOut.reserve(aGot);
120 for (size_t i = 0; i < aGot; ++i)
121 {
122 aOut.emplace_back(aIds[i]);
123 }
124 return aOut;
125}
126
128inline std::vector<FormatInfo> format_infos()
129{
130 size_t aCount = 0;
132
133 std::vector<FormatInfo> aOut;
134 aOut.reserve(aCount);
135 for (size_t anIndex = 0; anIndex < aCount; ++anIndex)
136 {
138 check(::occtl_de_format_info_at(anIndex, &aRaw));
139
140 size_t anExtCount = 0;
141 check(::occtl_de_format_extensions(aRaw.id, nullptr, 0, &anExtCount));
142 std::vector<const char*> aExts(anExtCount);
143 size_t anExtGot = 0;
144 check(::occtl_de_format_extensions(aRaw.id, aExts.data(), aExts.size(), &anExtGot));
145
146 FormatInfo anInfo;
147 anInfo.id = aRaw.id != nullptr ? aRaw.id : "";
148 anInfo.label = aRaw.label != nullptr ? aRaw.label : "";
149 anInfo.can_read_file = aRaw.can_read_file != 0;
150 anInfo.can_write_file = aRaw.can_write_file != 0;
151 anInfo.can_read_memory = aRaw.can_read_memory != 0;
152 anInfo.can_write_memory = aRaw.can_write_memory != 0;
153 anInfo.extensions.reserve(anExtGot);
154 for (size_t anExtIndex = 0; anExtIndex < anExtGot; ++anExtIndex)
155 {
156 anInfo.extensions.emplace_back(aExts[anExtIndex]);
157 }
158 aOut.emplace_back(std::move(anInfo));
159 }
160 return aOut;
161}
162
164inline FormatInfo format_info(const std::string& theFormatId)
165{
167 check(::occtl_de_format_info_by_id(theFormatId.c_str(), &aRaw));
168
169 size_t anExtCount = 0;
170 check(::occtl_de_format_extensions(aRaw.id, nullptr, 0, &anExtCount));
171 std::vector<const char*> aExts(anExtCount);
172 size_t anExtGot = 0;
173 check(::occtl_de_format_extensions(aRaw.id, aExts.data(), aExts.size(), &anExtGot));
174
175 FormatInfo anInfo;
176 anInfo.id = aRaw.id != nullptr ? aRaw.id : "";
177 anInfo.label = aRaw.label != nullptr ? aRaw.label : "";
178 anInfo.can_read_file = aRaw.can_read_file != 0;
179 anInfo.can_write_file = aRaw.can_write_file != 0;
180 anInfo.can_read_memory = aRaw.can_read_memory != 0;
181 anInfo.can_write_memory = aRaw.can_write_memory != 0;
182 anInfo.extensions.reserve(anExtGot);
183 for (size_t anExtIndex = 0; anExtIndex < anExtGot; ++anExtIndex)
184 {
185 anInfo.extensions.emplace_back(aExts[anExtIndex]);
186 }
187 return anInfo;
188}
189
191inline std::optional<std::string> format_id_for_path(const std::string& thePath)
192{
193 const char* aId = nullptr;
194 check(::occtl_de_format_id_from_path(thePath.c_str(), &aId));
195 if (aId == nullptr)
196 {
197 return std::nullopt;
198 }
199 return std::string(aId);
200}
201
202} // namespace occtl::de
203
204#endif // OCCTL_HPP_DE_HPP
RAII handle for a topology graph. Mirrors occtl_graph_t.
Definition topo.hpp:1444
::occtl_graph_t * get() const noexcept
Borrows-it pointer to the underlying C handle, for direct ABI calls.
Definition topo.hpp:1494
Session-local identity of a graph node. Mirrors occtl_node_id_t.
Definition topo.hpp:52
::occtl_node_id_t get() const noexcept
Borrows-it view of the underlying C value type, for direct ABI calls.
Definition topo.hpp:64
C++ veneer for the core module.
void check(const ::occtl_status_t theStatus)
Throw on non-OK; otherwise a no-op.
Definition core.hpp:85
std::pair< Graph, NodeId > read_memory(const std::string &theFormatId, const uint8_t *const theData, const std::size_t theSize)
Reads a memory payload using an explicit format id and returns (graph, root).
Definition de.hpp:62
FormatInfo format_info(const std::string &theFormatId)
Returns a descriptor for one supported data-exchange format.
Definition de.hpp:164
std::vector< uint8_t > write_memory(const Graph &theGraph, const NodeId theRoot, const std::string &theFormatId)
Writes a graph root into a memory payload using an explicit format id.
Definition de.hpp:88
void write(const Graph &theGraph, const NodeId theRoot, const std::string &thePath)
Writes a graph root to a file dispatched by extension.
Definition de.hpp:81
std::vector< FormatInfo > format_infos()
Returns rich descriptors for every supported data-exchange format.
Definition de.hpp:128
std::pair< Graph, NodeId > read(const std::string &thePath)
Reads a file dispatched by extension and returns (graph, root).
Definition de.hpp:52
std::optional< std::string > format_id_for_path(const std::string &thePath)
Returns the format id for thePath, or std::nullopt if no extension matches.
Definition de.hpp:191
std::vector< std::string > supported_formats()
Returns the stable lowercase format ids the build supports.
Definition de.hpp:111
OCCT-Light: unified data-exchange dispatch.
occtl_status_t occtl_de_format_ids(const char **out_format_ids, size_t cap, size_t *out_count)
occtl_status_t occtl_de_format_extensions(const char *format_id, const char **out_extensions, size_t cap, size_t *out_count)
occtl_status_t occtl_de_read(const char *path, occtl_graph_t **out_graph, occtl_node_id_t *out_root)
occtl_status_t occtl_de_format_info_at(size_t index, occtl_de_format_info_t *out_info)
occtl_status_t occtl_de_format_id_from_path(const char *path, const char **out_format_id)
occtl_status_t occtl_de_format_info_by_id(const char *format_id, occtl_de_format_info_t *out_info)
occtl_status_t occtl_de_write_memory(const occtl_graph_t *graph, occtl_node_id_t root, const char *format_id, uint8_t *out_data, size_t capacity, size_t *out_size)
occtl_status_t occtl_de_read_memory(const char *format_id, const uint8_t *data, size_t size, occtl_graph_t **out_graph, occtl_node_id_t *out_root)
occtl_status_t occtl_de_write(const occtl_graph_t *graph, occtl_node_id_t root, const char *path)
#define OCCTL_DE_FORMAT_INFO_INIT
Definition occtl_de.h:73
occtl_status_t occtl_de_format_count(size_t *out_count)
struct occtl_graph occtl_graph_t
Definition occtl_topo_types.h:152
Definition de.hpp:40
Definition occtl_de.h:58
int32_t can_read_file
Definition occtl_de.h:64
const char * id
Definition occtl_de.h:61
int32_t can_read_memory
Definition occtl_de.h:66
int32_t can_write_file
Definition occtl_de.h:65
const char * label
Definition occtl_de.h:62
int32_t can_write_memory
Definition occtl_de.h:67
Definition occtl_topo_types.h:50
C++ veneer for the topo module.