Next: , Up: Functions   [Contents][Index]


7.1 Decoding

The highest level function for decoding a file is dwg_read_file.

Function: int dwg_read_file (char *filename, Dwg_Data *dwg)

Open filename and decode it, saving information into dwg. Return 0 if successful.

You can then iterate over the entities in model space or paper space via two ways:

1. by using the dwg.h data structures. Via dwg->object[0], which is of type Dwg_Object_BLOCK_CONTROL, and a custom void process_BLOCK_HEADER(Dwg_Object_Ref* ref):

  Dwg_Object_BLOCK_CONTROL* block_control = dwg->block_control;
  // first all entities in the model space
  process_BLOCK_HEADER(dwg->header_vars.BLOCK_RECORD_MSPACE);
  // then all entities in the blocks
  for (i=0; i < block_control->num_entries; i++)
    {
      process_BLOCK_HEADER(block_control->block_headers[i]);
    }
  // and last all entities in the paper space
  process_BLOCK_HEADER(dwg->header_vars.BLOCK_RECORD_PSPACE);

or 2. by using the API functions from dwg_api.h:

  Dwg_Object_BLOCK_CONTROL* block_control = dwg_block_control(dwg);
  process_BLOCK_HEADER(dwg_model_space_ref(dwg));
  for (i=0; i < block_control->num_entries; i++)
    {
      process_BLOCK_HEADER(block_control->block_headers[i]);
    }
  process_BLOCK_HEADER(dwg_paper_space_ref(dwg));

and inside the process_BLOCK_HEADER function, you iterate over the entities from the block_header via:

  Dwg_Object* obj = get_first_owned_entity(ref->obj);
  while (obj)
    {
      process_object(obj);
      obj = get_next_owned_entity(ref->obj, obj);
    }

where process_object checks the type of each entity under the Dwg_Object* obj.

For each entity or object type (i.e. a non-graphical dwg object, also tables) there also exist the simple and expensive dwg_getall_ENTITY and dwg_getall_OBJECT functions:

Function: int dwg_getall_ENTITY (Dwg_Object_Ref *block_header_ref)

Return a malloc’ed NULL-terminated array of all such entities for Model Space, Paper Space or an individual block.

Function: int dwg_getall_OBJECT (Dwg_Data *dwg)

Return a malloc’ed NULL-terminated array of all such DWG objects.

The decoder is driven by the fields definition in the src/dwg.spec, which adds each field to the object. This is done in the src/decode.c or src/decode_r2007.c.

Function: int dwg_decode_OBJECT (Bit_Chain *dat, Dwg_Object *obj)

Sets the fields for the object from the DWG bitstream.

Note: Pre-R13 DWG’s do contain all deleted entities, which e.g. where moved into a BLOCK. Those entities do have a type > 127. You need to filter them out by yourself, when processing the DWG.


Next: Encoding, Up: Functions   [Contents][Index]