Importing DWG Sketchup 2019

,

stripping the layers for imports is not a good idea…

the geometry can be moved into groups, the group assigned to that layer and then have layer associations fixed…

LayerWatcher may also remove layers after imports [@TIG??] …

john

To remove all layers from selected geometry, or from geometry within selected ‘containers’ [component-instances and groups] - my LayerWatcher does have a context-menu tool ‘Selected-Geometry-To-Layer0’…

It also has several other useful layer-based tools too !

This snippet will gather ungrouped edges and faces by their assigned layer, create a group for each layer, transfer the entities to that group, and then reset them to use layer0 within the group. I wrote it to clean up models imported from CAD that use layers incorrectly for SketchUp. It preserves the structuring implied by the CAD layers but in a SketchUp-compatible way.

  # gather up loose edges and faces
  loose_faces = Sketchup.active_model.entities.grep(Sketchup::Face)
  loose_edges = Sketchup.active_model.entities.grep(Sketchup::Edge)
  # retain only edges not used by any face.  Edges needed by a face
  # will be handled along with the face.
  loose_free_edges = loose_edges.delete_if {|edge| !edge.faces.empty?}
  loose_geometry = loose_faces + loose_free_edges
  
  # Hash with a key for each layer used by loose geometry and value
  # an array of entities that use that layer.  Default initializes a
  # empty array so we can append items.
  loose_by_layer = Hash.new {|h,k| h[k] = []}
  loose_geometry.each {|entity| loose_by_layer[entity.layer] << entity}

  # create a group for each used layer and put the associated geometry
  # into it.  Because a face must have all its edges in the same context,
  # this will do two things: it will bring the bounding edges of the face
  # into the group even if they weren't using this layer, and it will create
  # a duplicate in the model if the edge is needed to bound a face still there.
  # These new edges in model aren't in the list we built earlier but aren't needed
  # because we captured only faces and free edges, not edges that bound faces.
  Sketchup.active_model.start_operation("Group by layer", true)
  loose_by_layer.keys.each do |layer|
    begin
      gp = Sketchup.active_model.entities.add_group(loose_by_layer[layer])
      # use the layer from the original loose entities for the group
      gp.layer = layer
      # and fix the Layer0 association of the ones in the group
      gp.definition.entities.each {|entity| entity.layer = nil}
    rescue => e
      # note: this reports the exception but lets the method
      # continue to process the rest of the layers
      UI.messagebox(e.message)
      puts e.message
      puts e.backtrace
    end
  end
  Sketchup.active_model.commit_operation
  nil
2 Likes