Class: Autocad::Drawing

Inherits:
Object
  • Object
show all
Includes:
Common, Faa::Cleanup
Defined in:
lib/autocad/drawing.rb,
lib/faa/cleanup.rb

Overview

Represents an AutoCAD drawing document and provides interface for:

  • File operations (save/open/close)

  • Space management (Model/Paper)

  • Layer/block/text management

  • Plot configuration

  • User interaction

  • Selection sets

  • System variables

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#method_missing

Methods included from Faa::Cleanup

#add_title_block, #cleanup_model, #cleanup_title_block, #faa_title_block, #fix_layout, #get_title_attributes, #remove_translation_text

Constructor Details

#initialize(app, ole, requested_name = nil) ⇒ Drawing

Initialize a new Drawing instance

Parameters:

  • app (Autocad::App)

    The application instance

  • ole (WIN32OLE)

    The OLE object for the drawing

  • requested_name (String, nil) (defaults to: nil)

    Optional name for the drawing before it’s saved



37
38
39
40
41
42
# File 'lib/autocad/drawing.rb', line 37

def initialize(app, ole, requested_name = nil)
  @app = app
  @ole_obj = ole
  @app_event = WIN32OLE_EVENT.new(ole)
  @requested_name = requested_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Autocad::Common

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/autocad/drawing.rb', line 20

def app
  @app
end

Class Method Details

.from_ole_obj(app, ole) ⇒ Object

Create a Drawing instance from an OLE object



26
27
28
# File 'lib/autocad/drawing.rb', line 26

def self.from_ole_obj(app, ole) #: Drawing
  new(app, ole)
end

Instance Method Details

#active_layerLayer

Get the active layer in the drawing

Returns:

  • (Layer)

    The active layer



286
287
288
289
# File 'lib/autocad/drawing.rb', line 286

def active_layer
  ole = ole_obj.ActiveLayer
  app.wrap(ole)
end

#active_layer=(layer) ⇒ Object

Set the active layer

Examples:

Set active layer by name

drawing.active_layer = "Walls"

Parameters:

  • layer (Layer, String)

    Layer object or name to make active

Raises:

  • (RuntimeError)

    If layer not found



304
305
306
307
308
309
310
311
312
# File 'lib/autocad/drawing.rb', line 304

def active_layer=(layer)
  if layer.is_a?(String)
    layer = ole_obj.Layers.Item(layer)
  elsif layer.is_a?(Autocad::Layer)
    layer = layer.to_ole
  end
  raise "layer not found" unless layer
  ole_obj.ActiveLayer = layer.to_ole
end

#active_layer_nameString

Get the name of the active layer

Returns:

  • (String)

    The name of the active layer



294
295
296
# File 'lib/autocad/drawing.rb', line 294

def active_layer_name
  ole_obj.ActiveLayer.Name
end

#active_layoutLayout

Get the active layout

Returns:

  • (Layout)

    The active layout



336
337
338
339
# File 'lib/autocad/drawing.rb', line 336

def active_layout
  ole = ole_obj.ActiveLayout
  app.wrap(ole)
end

#active_layout=(layout) ⇒ void

This method returns an undefined value.

Set the active layout

Examples:

Switch to a specific layout

layout = drawing.layouts.find { |l| l.name == "Layout1" }
drawing.active_layout = layout

Parameters:

  • layout (Layout)

    Layout to make active



348
349
350
# File 'lib/autocad/drawing.rb', line 348

def active_layout=(layout)
  ole_obj.ActiveLayout = layout.to_ole
end

#active_pviewportPViewport

Get the active paper space viewport

Returns:



326
327
328
329
330
331
# File 'lib/autocad/drawing.rb', line 326

def active_pviewport
  ole = ole_obj.ActivePViewport
  app.wrap(ole)
rescue => ex
  app.error_proc.call(ex, self)
end

#active_pviewport=(vport) ⇒ void

This method returns an undefined value.

Set the active paper space viewport

Parameters:



319
320
321
# File 'lib/autocad/drawing.rb', line 319

def active_pviewport=(vport)
  ole_obj.ActivePViewport = vport.to_ole
end

#active_spaceModelSpace, PaperSpace

Get the currently active space (model or paper)

Returns:



354
355
356
357
358
359
360
361
362
363
# File 'lib/autocad/drawing.rb', line 354

def active_space
  ole = ole_obj.ActiveSpace

  if ole == ACAD::AcPaperSpace
    paper_space

  else
    model_space
  end
end

#add_plot_configuration(name, model: false) ⇒ PlotConfiguration

Add a new plot configuration to the drawing

Examples:

Create a new PDF plot configuration

pdf_config = drawing.add_plot_configuration("PDF_Export")
pdf_config.device_name = "AutoCAD PDF.pc3"

Parameters:

  • name (String)

    Name for the new plot configuration

  • model (Boolean) (defaults to: false)

    Whether this is for model space (true) or paper space (false)

Returns:



162
163
164
165
166
167
168
169
# File 'lib/autocad/drawing.rb', line 162

def add_plot_configuration(name, model: false)
  plot_config = plot_configurations.find { |p| p.name == name }
  return plot_config if plot_config
  ole = @ole_obj.PlotConfigurations.Add(name, model)
  app.wrap(ole)
rescue => ex
  app.error_proc.call(ex, self)
end

#basenamePathname

Get the basename of the drawing as a Pathname

Returns:

  • (Pathname)

    The name as Pathname



265
266
267
# File 'lib/autocad/drawing.rb', line 265

def basename
  Pathname.new(name)
end

#block_reference_selection_setSelectionSetAdapter

Get a selection set adapter for block references

Examples:

Use the selection set to filter block references

ss = drawing.block_reference_selection_set
ss.filter { |f| f.and(f.layer("Furniture"), f.block_reference) }

Returns:



621
622
623
# File 'lib/autocad/drawing.rb', line 621

def block_reference_selection_set
  @block_reference_selection_set ||= get_block_reference_selection_set
end

#block_references {|BlockReference| ... } ⇒ Enumerator<BlockReference>

Get all block references in the drawing &: (BlockReference) -> void

Examples:

Count references by block name

counts = Hash.new(0)
drawing.block_references { |br| counts[br.name] += 1 }

Yields:

Returns:



581
582
583
584
585
586
587
588
589
# File 'lib/autocad/drawing.rb', line 581

def block_references
  return to_enum(__callee__) unless block_given?
  ss = block_reference_selection_set
  ss.clear
  ss.select
  ss.each do |o|
    yield o
  end
end

#blocks {|Block| ... } ⇒ Enumerator<Block>

Get all blocks in the drawing

Examples:

Iterate through all blocks

drawing.blocks.each { |block| puts block.name }

Yields:

  • (Block)

    Optional block to process each block

Returns:



415
416
417
418
# File 'lib/autocad/drawing.rb', line 415

def blocks
  return to_enum(__callee__) unless block_given?
  ole_obj.Blocks.each { |o| yield app.wrap(o) }
end

#close(save = true) ⇒ void

This method returns an undefined value.

Close the drawing

Examples:

Close without saving

drawing.close(false)

Parameters:

  • save (Boolean) (defaults to: true)

    Whether to save changes before closing

Raises:



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/autocad/drawing.rb', line 373

def close(save = true)
  # Store the name before marking as closed
  drawing_name = @ole_obj.respond_to?(:Name) ? @ole_obj.Name : "unknown"
  @drawing_closed = true

  begin
    if @ole_obj.respond_to?(:Close)
      @ole_obj.Close(save)
    elsif @ole_obj.respond_to?(:close)
      @ole_obj.close(save)
    else
      # If we can't close it directly, try through the app
      app.close_drawing(drawing_name, save)
    end
  rescue => ex
    # Instead of just calling error_proc, raise a specific DrawingClose error
    # Use drawing name instead of the drawing object
    raise DrawingClose.new("Failed to close drawing: #{ex.message}", drawing_name)
  ensure
    @ole_obj = nil
  end
end

#copy(name: nil, dir: nil) ⇒ void

This method returns an undefined value.

Copy the drawing to a new file

Examples:

Create a backup copy

drawing.copy(name: "backup_#{Time.now.strftime('%Y%m%d')}.dwg")

Parameters:

  • name (String, Pathname, nil) (defaults to: nil)

    Name of the file

  • dir (String, Pathname, nil) (defaults to: nil)

    Target directory



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/autocad/drawing.rb', line 203

def copy(name: nil, dir: nil) #: void
  if dir.nil?
    lname = name || copy_name
    dir_path = dirname
  else
    lname = name || self.name
    dir_path = Pathname.new(dir)
  end
  copy_path = dir_path + lname
  FileUtils.copy path.to_s, copy_path.to_s, verbose: true
end

#create_layer(name, color = nil) ⇒ Acad::Layer

Create a new layer or get an existing one

Examples:

Create a red layer

walls_layer = drawing.create_layer("Walls", :red)

Create a layer with a specific color index

detail_layer = drawing.create_layer("Details", 42)

Parameters:

  • name (String, Layer)

    Layer name to create or existing Layer object

  • color (Integer, Symbol, String, nil) (defaults to: nil)

    Color for the layer (can be ACAD::COLOR constant, symbol, or integer)

Returns:

  • (Acad::Layer)

    The created or existing layer



470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/autocad/drawing.rb', line 470

def create_layer(name, color = nil)
  if name.class == Autocad::Layer
    name.color = Autocad.color_to_index(color) if color
    return name
  end
  ole_layer = begin
    ole_obj.Layers.Item(name)
  rescue
    nil
  end
  ole_layer ||= ole_obj.Layers.Add(name)
  ole_layer.Color = Autocad.color_to_index(color) if color
  app.wrap(ole_layer)
end

#create_selection_set(name) {|SelectionSetAdapter| ... } ⇒ SelectionSetAdapter

Create a new selection set in the drawing

Examples:

Create a selection set with a filter

create_selection_set("walls") do |ss|
  ss.filter.layer("WALLS").and.block_reference
end

Parameters:

  • name (String)

    The name for the selection set

Yields:

Returns:



61
62
63
64
65
# File 'lib/autocad/drawing.rb', line 61

def create_selection_set(name)
  ss = SelectionSet.new(name)
  yield ss if block_given?
  SelectionSetAdapter.new(self, ss)
end

#dim_styles {|DimStyle| ... } ⇒ Enumerator<DimStyle>

Get all dimension styles in the drawing &: (DimStyle) -> void

Examples:

Find architectural dimension style

arch_style = drawing.dim_styles.find { |ds| ds.name == "ARCHITECTURAL" }

Yields:

  • (DimStyle)

    Optional block to process each dimension style

Returns:



598
599
600
601
# File 'lib/autocad/drawing.rb', line 598

def dim_styles
  return to_enum(__callee__) unless block_given?
  ole_obj.DimStyles.each { |o| yield app.wrap(o) }
end

#dirnamePathname

Get the directory of the drawing

Returns:

  • (Pathname)

    The directory of the file



272
273
274
# File 'lib/autocad/drawing.rb', line 272

def dirname
  Pathname.new(ole_obj.Path).expand_path
end

#event_handlerEventHandler

Get the event handler for this drawing

Returns:



47
48
49
# File 'lib/autocad/drawing.rb', line 47

def event_handler #: EventHandler
  @event_handler ||= default_event_handler
end

#get_float(prompt: "Enter a float") ⇒ Float

Get a floating point input from the user

Examples:

Get a scale factor

scale = drawing.get_float(prompt: "Enter scale factor:")

Parameters:

  • prompt (String) (defaults to: "Enter a float")

    The string to prompt the user

Returns:

  • (Float)

    User input

Raises:



771
772
773
774
775
# File 'lib/autocad/drawing.rb', line 771

def get_float(prompt: "Enter a float")
  utility.GetReal(prompt)
rescue => ex
  raise Autocad::Error.new("Error getting float input from user #{ex}")
end

#get_input_integer(prompt: "Enter a integer") ⇒ Integer

Get an integer input from the user

Examples:

Get number of copies

copies = drawing.get_input_integer(prompt: "Enter number of copies:")

Parameters:

  • prompt (String) (defaults to: "Enter a integer")

    The string to prompt the user

Returns:

  • (Integer)

    User input

Raises:



759
760
761
762
763
# File 'lib/autocad/drawing.rb', line 759

def get_input_integer(prompt: "Enter a integer")
  utility.GetInteger(prompt)
rescue => ex
  raise Autocad::Error.new("Error getting integer input from user #{ex}")
end

#get_input_string(prompt: "Enter a string", spaces: true) ⇒ String

Get a string input from the user

Examples:

Get a filename from user

filename = drawing.get_input_string(prompt: "Enter filename:", spaces: false)

Parameters:

  • prompt (String) (defaults to: "Enter a string")

    The string to prompt the user

  • spaces (Boolean) (defaults to: true)

    Whether the string returned can contain spaces

Returns:

  • (String)

    User input

Raises:



746
747
748
749
750
# File 'lib/autocad/drawing.rb', line 746

def get_input_string(prompt: "Enter a string", spaces: true)
  utility.GetString(spaces, prompt)
rescue => ex
  raise Autocad::Error.new("Error getting string input from user #{ex}")
end

#get_point(prompt: "Get point", base_point: nil) ⇒ Point3d

Note:

If base_point is provided, a stretched line is drawn from the base point

Prompt the user for a point in the drawing

Examples:

Get start and end points for a line

start = drawing.get_point(prompt: "Select start point:")
end_pt = drawing.get_point(prompt: "Select end point:", base_point: start)
drawing.model.add_line(start, end_pt)

Parameters:

  • prompt (String) (defaults to: "Get point")

    The prompt string to display

  • base_point (Array, Point3d, nil) (defaults to: nil)

    Optional base point for rubber-band line

Returns:

Raises:



790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/autocad/drawing.rb', line 790

def get_point(prompt: "Get point", base_point: nil)
  if base_point
    array_pt = base_point.to_ary.map { |x| x.to_f } unless base_point.nil?
    base_point = WIN32OLE_VARIANT.array([3], WIN32OLE::VARIANT::VT_R8)
    base_point[0] = array_pt[0]
    base_point[1] = array_pt[1]
    base_point[2] = array_pt[2]
  end
  pt = utility.GetPoint(base_point, prompt)
  Point3d.new(pt[0], pt[1], pt[2])
rescue => ex
  raise Autocad::Error.new("Error getting point input from user #{ex}")
end

#get_regionArray<Point3d>

Prompt the user to select a rectangular region

Examples:

Get a region for a window selection

corner1, corner2 = drawing.get_region
# Use corners for window selection

Returns:

  • (Array<Point3d>)

    Two points defining opposite corners of the region



809
810
811
812
813
814
815
# File 'lib/autocad/drawing.rb', line 809

def get_region
  pt = get_point(prompt: "Specify first corner")
  prompt("X: #{pt.x}, Y: #{pt.y}, Z: #{pt.z}\n")
  point2 = utility.GetCorner(pt.to_ole, "Specify opposite corner: ")
  pt2 = Point3d(point2)
  [pt, pt2]
end

#get_select_text_containing(str) ⇒ SelectionSetAdapter

Get a selection set containing text with the specified string

Parameters:

  • str (String)

    Text pattern to search for

Returns:



561
562
563
564
565
566
567
568
569
570
571
# File 'lib/autocad/drawing.rb', line 561

def get_select_text_containing(str)
  name = "text_containing_#{str}"
  varname = "@#{name}".tr("*", "_")
  ss = get_selection_set(name)
  ss.delete if ss
  ss = create_selection_set(name) do |ss|
    ss.filter_text_containing(str)
  end
  instance_variable_set(varname, ss)
  ss
end

#get_selection_set(name) ⇒ SelectionSet?

Get a selection set by name

Examples:

Get or create a selection set

ss = drawing.get_selection_set("my_selection") || drawing.create_selection_set("my_selection")

Parameters:

  • name (String)

    Selection set name to return

Returns:

  • (SelectionSet, nil)

    The selection set or nil if not found



403
404
405
406
# File 'lib/autocad/drawing.rb', line 403

def get_selection_set(name)
  ole = get_ole_selection_set(name)
  app.wrap(ole) if ole
end

#get_variable(name) ⇒ Object

Get the value of a system variable

Examples:

Get current layer

current_layer = drawing.get_variable("CLAYER")

Get dimension scale

dim_scale = drawing.get_variable("DIMSCALE")

Parameters:

  • name (String)

    The name of the system variable

Returns:

  • (Object)

    The value of the system variable



634
635
636
# File 'lib/autocad/drawing.rb', line 634

def get_variable(name)
  ole_obj.GetVariable(name)
end

#get_variables(*atts) ⇒ Array<Object>

Get the values of multiple system variables

Examples:

Get multiple dimension settings

scale, arrow_size = drawing.get_variables("DIMSCALE", "DIMASZ")

Parameters:

  • *atts (Array<String>)

    The names of the system variables

Returns:

  • (Array<Object>)

    The values of the system variables



705
706
707
708
709
710
711
712
713
714
# File 'lib/autocad/drawing.rb', line 705

def get_variables(*atts)
  return [] if atts.empty?
  if atts.first.class == Array
    atts = atts.first
  end
  atts.each_with_object([]) do |k, a|
    a << ole_obj.GetVariable(k)
    a
  end
end

#has_pviewport?Boolean

Check if the drawing has any paper space viewports

Returns:

  • (Boolean)

    True if paper space has at least one viewport



554
555
556
# File 'lib/autocad/drawing.rb', line 554

def has_pviewport?
  paper_space.pviewports.count > 0
end

#layers {|Layer| ... } ⇒ Enumerator<Layer>

Get all layers in the drawing

Examples:

Find frozen layers

frozen_layers = drawing.layers.select { |layer| layer.frozen? }

Yields:

  • (Layer)

    Optional block to process each layer

Returns:



439
440
441
442
443
# File 'lib/autocad/drawing.rb', line 439

def layers
  return to_enum(__callee__) unless block_given?

  ole_obj.Layers.each { |o| yield app.wrap(o) }
end

#layouts {|Layout| ... } ⇒ Enumerator<Layout>

Get all layouts in the drawing

Examples:

Find a layout by name

title_layout = drawing.layouts.find { |layout| layout.name == "Title Block" }

Yields:

  • (Layout)

    Optional block to process each layout

Returns:



427
428
429
430
# File 'lib/autocad/drawing.rb', line 427

def layouts
  return to_enum(__callee__) unless block_given?
  ole_obj.Layouts.each { |o| yield app.wrap(o) }
end

#linetypes {|Linetype| ... } ⇒ Enumerator<Linetype>

Get all linetypes in the drawing

Examples:

Load a new linetype

unless drawing.linetypes.any? { |lt| lt.name == "DASHED" }
  drawing.load_linetype("acad.lin", "DASHED")
end

Yields:

  • (Linetype)

    Optional block to process each linetype

Returns:



454
455
456
457
# File 'lib/autocad/drawing.rb', line 454

def linetypes
  return to_enum(__callee__) unless block_given?
  ole_obj.Linetypes.each { |o| yield app.wrap(o) }
end

#model_block_referencesEnumerator<BlockReference>

Get all block references in model space

Examples:

Find all title blocks in model space

title_blocks = drawing.model_block_references.select { |br| br.name == "TITLE_BLOCK" }

Returns:



506
507
508
509
510
511
512
513
514
515
# File 'lib/autocad/drawing.rb', line 506

def model_block_references
  ss = selection_sets.find { it.name == "model_block_references" }
  ss ||= create_selection_set("model_block_references")
  ss.filter do |f|
    f.and(f.model_space, f.block_reference)
  end
  ss.clear
  ss.select
  ss.each
end

#model_spaceModelSpace Also known as: model

Get the model space of the drawing

Examples:

Add a circle to model space

drawing.model_space.add_circle([0,0,0], 10)

Returns:



834
835
836
# File 'lib/autocad/drawing.rb', line 834

def model_space
  app.wrap ole_obj.ModelSpace
end

#model_space?Boolean

Check if model space is active

Returns:

  • (Boolean)

    True if model space is active



225
226
227
# File 'lib/autocad/drawing.rb', line 225

def model_space? #: bool
  ole_obj.ActiveSpace == ACAD::AcModelSpace
end

#modified?Boolean

Check if the drawing has been modified since the last save

Returns:

  • (Boolean)

    True if drawing is modified



95
96
97
# File 'lib/autocad/drawing.rb', line 95

def modified?
  ole_obj.Saved == false
end

#nameString

Returns the name of the drawing If the drawing hasn’t been saved yet, returns the requested name

Returns:

  • (String)

    The name of the drawing with .dwg extension



253
254
255
256
257
258
259
260
# File 'lib/autocad/drawing.rb', line 253

def name
  if @requested_name && !previously_saved?
    # Make sure we return just the basename with .dwg extension
    basename = File.basename(@requested_name)
    return basename.end_with?('.dwg') ? basename : "#{basename}.dwg"
  end
  ole_obj.Name
end

#ole_objWIN32OLE

Get the underlying OLE object

Returns:

  • (WIN32OLE)

    The OLE object for the drawing

Raises:



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/autocad/drawing.rb', line 876

def ole_obj
  if @drawing_closed || @ole_obj.nil?
    # Use a local variable to avoid recursive call to name method
    drawing_name = @ole_obj.respond_to?(:Name) ? @ole_obj.Name : "unknown"
    raise DrawingClose.new("Drawing is closed", drawing_name)
  end

  # Check if the ole object is still valid
  begin
    @ole_obj.Name
    @ole_obj
  rescue => e
    @drawing_closed = true
    @ole_obj = nil
    # Use a string directly instead of calling name method
    raise DrawingClose.new("Drawing is no longer valid: #{e.message}", "unknown")
  end
end

#paper_block_referencesEnumerator<BlockReference>

Get all block references in paper space

Examples:

Delete all border blocks in paper space

drawing.paper_block_references.each do |br|
  br.delete if br.name == "BORDER"
end

Returns:



524
525
526
527
528
529
530
531
532
533
# File 'lib/autocad/drawing.rb', line 524

def paper_block_references
  ss = selection_sets.find { it.name == "paper_block_references" }
  ss ||= create_selection_set("paper_block_references")
  ss.filter do |f|
    f.and(f.paper_space, f.block_reference)
  end
  ss.clear
  ss.select
  ss.each
end

#paper_spacePaperSpace Also known as: paper

Get the paper space of the drawing

Examples:

Add a viewport to paper space

drawing.paper_space.add_pv_viewport([5,5,0], width: 200, height: 150)

Returns:



865
866
867
# File 'lib/autocad/drawing.rb', line 865

def paper_space
  app.wrap ole_obj.PaperSpace
end

#paper_space?Boolean

Check if paper space is active

Returns:

  • (Boolean)

    True if paper space is active



218
219
220
# File 'lib/autocad/drawing.rb', line 218

def paper_space? #: bool
  ole_obj.ActiveSpace == ACAD::AcPaperSpace
end

#paper_space_layoutLayout

Get the first layout that is not “Model”

Returns:

  • (Layout)

    The first paper space layout



191
192
193
# File 'lib/autocad/drawing.rb', line 191

def paper_space_layout
  layouts.reject { it.name == "Model" }.first
end

#pathPathname

Get the full path of the drawing

Returns:

  • (Pathname)

    The complete path of file



279
280
281
# File 'lib/autocad/drawing.rb', line 279

def path
  dirname + basename
end

#pdf_plot_configPlotConfiguration

Get the predefined PDF plot configuration “faa_ansid_bw” Creates it if it doesn’t exist

Returns:



175
176
177
# File 'lib/autocad/drawing.rb', line 175

def pdf_plot_config #: PlotConfiguration
  @pdf_plot_config ||= create_pdf_plot_configutation
end

#plotPlot

Get the plot object for this drawing

Returns:

  • (Plot)

    The plot object



182
183
184
185
186
# File 'lib/autocad/drawing.rb', line 182

def plot #: Plot
  ole = ole_obj.Plot
  ole.QuietErrorMode = true
  app.wrap(ole_obj.Plot)
end

#plot_configurations {|Layout| ... } ⇒ Enumerator<Layout>

Get all layouts in the drawing (duplicate method, should be removed)

Yields:

  • (Layout)

    Optional block to process each layout

Returns:



845
846
847
848
# File 'lib/autocad/drawing.rb', line 845

def plot_configurations
  return to_enum(__callee__) unless block_given?
  ole_obj.PlotConfigurations.each { |o| yield app.wrap(o) }
end

#previously_saved?Boolean

Check if the drawing has been saved before

Returns:

  • (Boolean)

    True if drawing is previously saved



88
89
90
# File 'lib/autocad/drawing.rb', line 88

def previously_saved?
  ole_obj.FullName != ""
end

#prompt(message) ⇒ void

This method returns an undefined value.

Display a message in the AutoCAD command line

Examples:

Show a status message

drawing.prompt("Processing complete. Select objects to continue.")

Parameters:

  • message (String)

    The message to display



733
734
735
# File 'lib/autocad/drawing.rb', line 733

def prompt(message)
  utility.Prompt(message)
end

#read_only?Boolean

Check if the drawing is read-only

Returns:

  • (Boolean)

    True if drawing is read only



81
82
83
# File 'lib/autocad/drawing.rb', line 81

def read_only?
  ole_obj.ReadOnly
end

#regen(view_ports = :all) ⇒ void

Note:

Uses AcRegenType enum from AutoCAD

This method returns an undefined value.

Regenerate the drawing display

Examples:

Regenerate all viewports

drawing.regen(:all)

Parameters:

  • view_ports (Symbol) (defaults to: :all)

    Viewports to regenerate (:all or :active)



493
494
495
496
497
498
499
# File 'lib/autocad/drawing.rb', line 493

def regen(view_ports = :all)
  vp_type = case view_ports
  when :all then 1
  when :active then 0
  end
  ole_obj.Regen vp_type
end

#register_handler(event) { ... } ⇒ Object

Register an event handler

Examples:

Register a handler for BeginCommand event

register_handler("BeginCommand") { puts "Command started" }

Parameters:

  • event (String)

    Event key for handler

Yields:

  • Handler procedure to execute when event occurs



74
75
76
# File 'lib/autocad/drawing.rb', line 74

def register_handler(event, &) #:void
  @event_handler.add_handler(event, &) unless event == "OnQuit"
end

#save(name: nil, dir: nil) ⇒ void

This method returns an undefined value.

Saves the drawing If the drawing hasn’t been saved yet and no name is provided, uses the requested name

Examples:

Save drawing with new name in specific directory

drawing.save(name: "floor_plan_v2", dir: "C:/Projects/Building")

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional new name for the drawing

  • dir (String, Pathname, nil) (defaults to: nil)

    Optional directory to save to



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/autocad/drawing.rb', line 109

def save(name: nil, dir: nil) #: void
  return if read_only?

  # Use requested_name if no name is provided and drawing hasn't been saved yet
  name ||= @requested_name if !previously_saved? && @requested_name

  if previously_saved? && modified? && !name && !dir
    ole_obj.Save
    puts "saved #{path}"
  else
    out_name = dwg_path(name: name, dir: dir)
    windows_name = app.windows_path(out_name)
    ole_obj.SaveAs(windows_name)
    puts "saved #{windows_name}"
    # After saving, clear the requested name since it's now saved
    @requested_name = nil
  end
end

#save_as_pdf(name: nil, dir: nil, model: false) ⇒ void

This method returns an undefined value.

Save the drawing as a PDF file If name or directory is given, uses those params Otherwise uses the drawing name and directory

Examples:

Export to PDF with custom name

drawing.save_as_pdf(name: "presentation", dir: "C:/Exports")

Parameters:

  • name (String, nil) (defaults to: nil)

    The name of the PDF file

  • dir (String, Pathname, nil) (defaults to: nil)

    The directory to save the PDF

  • model (Boolean) (defaults to: false)

    Whether to use model space (true) or paper space (false)



139
140
141
142
143
# File 'lib/autocad/drawing.rb', line 139

def save_as_pdf(name: nil, dir: nil, model: false) #: void
  out_name = pdf_path(name: name, dir: dir)
  print_pdf(out_name, model:)
  puts "saved #{out_name}"
end

#select_text_containing(str) ⇒ Enumerator<Element>

Select all text objects containing the specified string

Examples:

Find all text containing “REVISION”

revision_texts = drawing.select_text_containing("*REVISION*")

Parameters:

  • str (String)

    Text pattern to search for (supports wildcards)

Returns:



540
541
542
543
544
545
546
547
548
549
550
# File 'lib/autocad/drawing.rb', line 540

def select_text_containing(str)
  varname = "@text_containg_#{str}".tr("*", "_")
  ss = if instance_variable_defined?(varname)
    instance_variable_get(varname)
  else
    get_select_text_containing(str)
  end
  ss.clear
  ss.select
  ss.each
end

#selection_sets {|SelectionSetAdapter| ... } ⇒ Enumerator<SelectionSetAdapter>

Get all selection sets in the drawing

Examples:

Find a specific selection set

walls_ss = drawing.selection_sets.find { |ss| ss.name == "WALLS" }

Yields:

Returns:



824
825
826
827
# File 'lib/autocad/drawing.rb', line 824

def selection_sets
  return to_enum(__callee__) unless block_given?
  ole_selection_sets.each { |o| yield app.wrap(o) }
end

#set_variable(name, value) ⇒ void

This method returns an undefined value.

Set the value of a system variable

Examples:

Set dimension scale

drawing.set_variable("DIMSCALE", 2.5)

Set current layer

drawing.set_variable("CLAYER", "Dimensions")

Parameters:

  • name (String)

    The name of the system variable

  • value (Object)

    The value to set



649
650
651
# File 'lib/autocad/drawing.rb', line 649

def set_variable(name, value)
  ole_obj.SetVariable(name, value)
end

#set_variables(names, values) ⇒ void

This method returns an undefined value.

Set multiple system variables at once

Examples:

Set multiple dimension variables

drawing.set_variables(
  ["DIMSCALE", "DIMLWD", "DIMCLRT"],
  [2.5, 2, Autocad::Color::Blue]
)

Parameters:

  • names (Array<String>)

    The names of the system variables

  • values (Array<Object>)

    The values to set



665
666
667
668
669
670
# File 'lib/autocad/drawing.rb', line 665

def set_variables(names, values)
  atts = names.zip(values).to_h
  atts.each do |k, v|
    set_variable(k, v)
  end
end

#text_styles {|TextStyle| ... } ⇒ Enumerator<TextStyle>

Get all text styles in the drawing &: (TextStyle) -> void

Examples:

Find a specific text style

romans = drawing.text_styles.find { |ts| ts.name == "Romans" }

Yields:

  • (TextStyle)

    Optional block to process each text style

Returns:



610
611
612
613
# File 'lib/autocad/drawing.rb', line 610

def text_styles
  return to_enum(__callee__) unless block_given?
  ole_obj.TextStyles.each { |o| yield app.wrap(o) }
end

#to_model_spacevoid

This method returns an undefined value.

Switch to model space

Examples:

Switch to model space and add geometry

drawing.to_model_space
drawing.model.add_circle([0,0,0], 10)


245
246
247
# File 'lib/autocad/drawing.rb', line 245

def to_model_space #: void
  ole_obj.ActiveSpace = ACAD::AcModelSpace
end

#to_paper_spacevoid

This method returns an undefined value.

Switch to paper space

Examples:

Switch to paper space and add a viewport

drawing.to_paper_space
viewport = drawing.paper.add_pv_viewport(center, width: 200, height: 150)


235
236
237
# File 'lib/autocad/drawing.rb', line 235

def to_paper_space #: void
  ole_obj.ActiveSpace = ACAD::AcPaperSpace
end

#view_centerPoint3d

Get the center point of the current view

Returns:

  • (Point3d)

    The center of the view in world coordinates



148
149
150
151
# File 'lib/autocad/drawing.rb', line 148

def view_center
  center = get_variable("VIEWCTR")
  Point3d(center)
end

#with_system_variables(names, values) { ... } ⇒ Object

Temporarily set system variables and restore them after the block executes

Examples:

Temporarily change text settings

drawing.with_system_variables(
  ["TEXTSTYLE", "TEXTSIZE"],
  ["Standard", 2.5]
) do
  # Add text with these settings
  drawing.model.add_text("Note", [0,0,0])
end

Parameters:

  • names (Array<String>)

    The names of the system variables

  • values (Array<Object>)

    The values to set

Yields:

  • Block to execute with the temporary variable values

Returns:

  • (Object)

    The result of the block



689
690
691
692
693
694
695
696
# File 'lib/autocad/drawing.rb', line 689

def with_system_variables(names, values, &block)
  atts
  current_values = get_variables(names)
  set_variables(names, values)
  yield
ensure
  set_variables(names, current_values)
end