openbim-ifc-cli 0.4.0

The openbim-ifc command: validate, convert, inspect, query and edit IFC files (STEP, ifcXML and ifcZIP), and exchange their values as CSV, TSV or XLSX tables, from a shell or CI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! The argument grammar.
//!
//! Every command takes its input as a path; a file whose content is not
//! STEP or ifcXML is refused before anything is read into a model. The
//! output format of a report is `--format`; `convert` takes its output
//! format from the output file's extension unless `--to` names it.

use std::path::PathBuf;

use clap::{Args, Parser, Subcommand, ValueEnum};

/// Validate, convert, inspect and query IFC files (STEP, ifcXML and ifcZIP).
///
/// Exit codes: 0 success, 1 findings (validate, lint), 2 usage error,
/// unreadable input or a refusal, 3 an edit refused (edit, import-table;
/// nothing written).
#[derive(Debug, Parser)]
#[command(
    name = "openbim-ifc",
    version,
    about,
    long_about = None,
    after_help = "Exit codes: 0 success, 1 findings (validate, lint), 2 usage error, \
                  unreadable input or a refusal, 3 an edit refused (edit, import-table; \
                  nothing written).\nGuide: https://openbimrs.github.io/ifc/guide/cli"
)]
pub(crate) struct Cli {
    #[command(subcommand)]
    pub(crate) command: Command,
}

/// The commands.
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
    /// Validate against the schema each file declares (exit 1 on findings).
    Validate(ValidateArgs),
    /// Convert between STEP, ifcXML (native or XSD layout) and ifcZIP.
    Convert(ConvertArgs),
    /// Show the header, the declared schema and entity counts.
    Info(InfoArgs),
    /// Show the property and quantity sets of one entity or type object.
    Psets(PsetsArgs),
    /// Show the spatial structure: project, site, building, storey, space.
    Tree(TreeArgs),
    /// Report products no model viewer will draw (exit 1 on findings).
    Lint(LintArgs),
    /// List the entities a selector selects, with values read by path.
    Select(SelectArgs),
    /// Convert the model's length unit, rescaling every length, area and
    /// volume, and write it as STEP.
    ConvertUnits(ConvertUnitsArgs),
    /// Set values by path on the entities a selector selects, as one
    /// checked transaction, into a new file (exit 3 on a refusal).
    Edit(EditArgs),
    /// Write the entities a selector selects as a table: one column per
    /// value path, keyed by GlobalId or id (CSV, TSV or XLSX).
    ExportTable(ExportTableArgs),
    /// Write a table's edited cells back by key, as one checked
    /// transaction, into a new file (exit 3 on a refusal).
    ImportTable(ImportTableArgs),
    /// Show what one entity is related to: container, parent, type,
    /// openings, materials, classifications, groups, and every
    /// relationship naming it.
    Relations(RelationsArgs),
    /// Show the material definition of each selected entity, with its
    /// layers, profiles or constituents.
    Materials(MaterialsArgs),
    /// Show every cost schedule with its tree of cost items, values and
    /// rolled-up totals.
    Cost(CostArgs),
    /// Show every work schedule and work plan with its tree of tasks and
    /// their scheduled times.
    Schedule(CostArgs),
}

/// How the input's ifcXML layout is chosen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum InputLayout {
    /// The native layout when the root element carries a `schema`
    /// attribute, else the XSD layout of the release whose namespace the
    /// root declares; anything else is refused.
    Auto,
    /// This library's own lossless layout.
    Native,
    /// The buildingSMART XSD configuration (IFC4 ADD2 TC1, IFC4X3 ADD2).
    Xsd,
}

/// Options shared by every command that reads one model.
#[derive(Debug, Clone, Args)]
pub(crate) struct InputArgs {
    /// The ifcXML layout of the input (STEP input ignores it).
    #[arg(long, value_enum, default_value_t = InputLayout::Auto)]
    pub(crate) input_layout: InputLayout,
}

/// Report formats of `validate` and `lint`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum ReportFormat {
    /// One line per finding, then a summary.
    Human,
    /// One JSON document.
    Json,
    /// SARIF 2.1.0, for code-scanning tools.
    Sarif,
}

/// `validate`.
#[derive(Debug, Args)]
pub(crate) struct ValidateArgs {
    /// Files to validate; each against the schema its own header declares.
    #[arg(required = true)]
    pub(crate) files: Vec<PathBuf>,
    /// Report format.
    #[arg(long, value_enum, default_value_t = ReportFormat::Human)]
    pub(crate) format: ReportFormat,
    /// Record at most this many findings per file; a truncated report
    /// fails the run, since its verdict is not final.
    #[arg(long, value_name = "N")]
    pub(crate) max_findings: Option<usize>,
    /// Fail the run on warnings too, not only on errors.
    #[arg(long)]
    pub(crate) deny_warnings: bool,
    /// Also list the rules this validator does not evaluate; they are
    /// counted in the summary either way and never fail the run.
    #[arg(long)]
    pub(crate) include_unsupported: bool,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// Output formats of `convert`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum ModelFormat {
    /// ISO 10303-21 (`.ifc`).
    Step,
    /// ISO 10303-28 (`.ifcxml`).
    Ifcxml,
    /// ifcZIP (`.ifczip`): one STEP or ifcXML document (`--inner`) in a
    /// ZIP archive.
    Ifczip,
}

/// The document an ifcZIP archive holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum InnerFormat {
    /// ISO 10303-21, as `<name>.ifc`.
    Step,
    /// ISO 10303-28, as `<name>.ifcXML`, in `--layout`.
    Ifcxml,
}

/// The ifcXML layout `convert` writes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum OutputLayout {
    /// This library's own lossless layout, any release.
    Native,
    /// The buildingSMART XSD configuration; IFC4 and IFC4X3_ADD2 only.
    Xsd,
}

/// `convert`.
#[derive(Debug, Args)]
pub(crate) struct ConvertArgs {
    /// The file to read (STEP, ifcXML or ifcZIP).
    pub(crate) input: PathBuf,
    /// The file to write, or `-` for standard output (needs `--to`).
    pub(crate) output: PathBuf,
    /// The output format; by default from the output extension (`.ifc`,
    /// `.step`, `.stp`, `.ifcxml`, `.xml` or `.ifczip`).
    #[arg(long, value_enum)]
    pub(crate) to: Option<ModelFormat>,
    /// The document an ifcZIP output holds [default: step].
    #[arg(long, value_enum)]
    pub(crate) inner: Option<InnerFormat>,
    /// The ifcXML layout to write [default: native].
    #[arg(long, value_enum)]
    pub(crate) layout: Option<OutputLayout>,
    /// Replace the output file if it exists.
    #[arg(long)]
    pub(crate) force: bool,
    #[command(flatten)]
    pub(crate) input_options: InputArgs,
}

/// Formats of the inspection commands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum TextFormat {
    /// Readable text.
    Human,
    /// One JSON document.
    Json,
}

/// `info`.
#[derive(Debug, Args)]
pub(crate) struct InfoArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    /// List the count of every entity type, not only the 20 most frequent.
    #[arg(long)]
    pub(crate) all_types: bool,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// Formats of `psets`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum TableFormat {
    /// An aligned text table.
    Table,
    /// One JSON document.
    Json,
    /// RFC 4180 CSV with a header row.
    Csv,
}

/// `psets`.
#[derive(Debug, Args)]
pub(crate) struct PsetsArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The entity: `#12`, `12`, or its 22-character `GlobalId`.
    pub(crate) entity: String,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TableFormat::Table)]
    pub(crate) format: TableFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `tree`.
#[derive(Debug, Args)]
pub(crate) struct TreeArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    /// List each container's elements, not only their count.
    #[arg(long)]
    pub(crate) elements: bool,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `lint`.
#[derive(Debug, Args)]
pub(crate) struct LintArgs {
    /// Files to check.
    #[arg(required = true)]
    pub(crate) files: Vec<PathBuf>,
    /// Report format.
    #[arg(long, value_enum, default_value_t = ReportFormat::Human)]
    pub(crate) format: ReportFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `select`.
#[derive(Debug, Args)]
pub(crate) struct SelectArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The selector, e.g. `IfcWall, Pset_WallCommon.IsExternal=TRUE +
    /// IfcSlab` (grammar 1.0; see the guide's selector page).
    pub(crate) query: String,
    /// A value path to show for each entity, e.g. `type.Name` or
    /// `Pset_WallCommon.FireRating`; repeat for several columns.
    #[arg(long = "value", value_name = "PATH")]
    pub(crate) values: Vec<String>,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TableFormat::Table)]
    pub(crate) format: TableFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `convert-units`.
#[derive(Debug, Args)]
pub(crate) struct ConvertUnitsArgs {
    /// The file to read.
    pub(crate) input: PathBuf,
    /// The STEP file to write, or `-` for standard output.
    pub(crate) output: PathBuf,
    /// The new length unit: `METRE`, `MILLIMETRE`, `CENTIMETRE` (any SI
    /// prefix), `inch`, `foot`, `yard`, `mile` or `US survey foot`.
    #[arg(long, value_name = "UNIT")]
    pub(crate) unit: String,
    /// Keep the area and volume units (and their values) instead of
    /// replacing them by the square and cube of the new length unit.
    #[arg(long)]
    pub(crate) keep_area_volume: bool,
    /// Keep replaced unit entities nothing else uses.
    #[arg(long)]
    pub(crate) keep_replaced_units: bool,
    /// Report what would change and write nothing.
    #[arg(long)]
    pub(crate) dry_run: bool,
    /// Report format, on standard error (standard output for a dry run).
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    /// Replace an existing output file.
    #[arg(long)]
    pub(crate) force: bool,
    #[command(flatten)]
    pub(crate) input_options: InputArgs,
}

/// Where an edited model is written.
#[derive(Debug, Clone, Args)]
pub(crate) struct OutputArgs {
    /// The file to write (`.ifc`, `.ifcxml` or `.ifczip`); the input is
    /// never changed unless `--in-place` asks for it.
    #[arg(short, long, value_name = "FILE", conflicts_with = "in_place")]
    pub(crate) output: Option<PathBuf>,
    /// Replace the input file itself, in its own format.
    #[arg(long)]
    pub(crate) in_place: bool,
    /// Plan and report the changes; write nothing.
    #[arg(long)]
    pub(crate) dry_run: bool,
    /// Replace the output file if it exists.
    #[arg(long)]
    pub(crate) force: bool,
}

/// `edit`.
#[derive(Debug, Args)]
pub(crate) struct EditArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The selector choosing the entities to edit, e.g. `IfcWall,
    /// type=WT-200` (grammar 1.0).
    pub(crate) query: String,
    /// A value path and the value to write there, e.g. `--set Name "Wall
    /// A"` or `--set Pset_WallCommon.FireRating F90`; repeat for several.
    /// The value is read as the target's type; `IFCLABEL(F90)` names one.
    #[arg(long = "set", num_args = 2, value_names = ["PATH", "VALUE"], required = true)]
    pub(crate) set: Vec<String>,
    /// The value text that writes `$` (NULL).
    #[arg(long, value_name = "TEXT", default_value = "$")]
    pub(crate) null: String,
    /// Report format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    #[command(flatten)]
    pub(crate) output: OutputArgs,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// Table file formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum SheetFormatArg {
    /// RFC 4180 CSV, UTF-8.
    Csv,
    /// Tab-separated values, UTF-8, with `\t`, `\n` and `\\` escapes.
    Tsv,
    /// An Office Open XML workbook; every cell is text.
    Xlsx,
}

/// The column rows are matched by.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum KeyArg {
    /// `GlobalId`, stable across tools.
    GlobalId,
    /// `id`, the entity's `#id` in this file.
    Id,
}

/// `export-table`.
#[derive(Debug, Args)]
pub(crate) struct ExportTableArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The selector choosing the rows, e.g. `IfcWall + IfcSlab`.
    pub(crate) query: String,
    /// A value path per column, e.g. `Name`, `type.Name`,
    /// `Pset_WallCommon.FireRating`, `container.Name`; repeat for several
    /// [default: class, Name].
    #[arg(long = "column", short = 'c', value_name = "PATH")]
    pub(crate) columns: Vec<String>,
    /// The table to write, or `-` for standard output.
    #[arg(short, long, value_name = "FILE", default_value = "-")]
    pub(crate) output: PathBuf,
    /// The table format; by default from the output extension (`.csv`,
    /// `.tsv`, `.xlsx`), else CSV.
    #[arg(long, value_enum)]
    pub(crate) to: Option<SheetFormatArg>,
    /// The key column, first in the table.
    #[arg(long, value_enum, default_value_t = KeyArg::GlobalId)]
    pub(crate) key: KeyArg,
    /// The cell text of `$` (NULL).
    #[arg(long, value_name = "TEXT", default_value = "")]
    pub(crate) null: String,
    /// Replace the output file if it exists.
    #[arg(long)]
    pub(crate) force: bool,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `import-table`.
#[derive(Debug, Args)]
pub(crate) struct ImportTableArgs {
    /// The model to read.
    pub(crate) file: PathBuf,
    /// The table to apply (CSV, TSV or XLSX).
    pub(crate) table: PathBuf,
    /// The table format; by default from its extension.
    #[arg(long, value_enum)]
    pub(crate) from: Option<SheetFormatArg>,
    /// The key column [default: GlobalId when the table has one, else id].
    #[arg(long, value_enum)]
    pub(crate) key: Option<KeyArg>,
    /// The cell text of `$` (NULL).
    #[arg(long, value_name = "TEXT", default_value = "")]
    pub(crate) null: String,
    /// Report format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    #[command(flatten)]
    pub(crate) output: OutputArgs,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `relations`.
#[derive(Debug, Args)]
pub(crate) struct RelationsArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The entity: `#12`, `12`, or its 22-character `GlobalId`.
    pub(crate) entity: String,
    /// List the chain of parents up to the project instead.
    #[arg(long)]
    pub(crate) traverse_up: bool,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `materials`.
#[derive(Debug, Args)]
pub(crate) struct MaterialsArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// The selector choosing the entities.
    #[arg(default_value = "IfcElement + IfcElementType")]
    pub(crate) query: String,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

/// `cost` and `schedule`.
#[derive(Debug, Args)]
pub(crate) struct CostArgs {
    /// The file to read.
    pub(crate) file: PathBuf,
    /// Expand at most this many levels of nested items or sub-tasks.
    #[arg(long, value_name = "N")]
    pub(crate) depth: Option<usize>,
    /// Output format.
    #[arg(long, value_enum, default_value_t = TextFormat::Human)]
    pub(crate) format: TextFormat,
    #[command(flatten)]
    pub(crate) input: InputArgs,
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory;

    #[test]
    fn the_grammar_is_consistent() {
        super::Cli::command().debug_assert();
    }
}