SHARE
TWEET

Untitled

a guest Jul 17th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'ffi'
  2.  
  3. module SketchupFFI
  4.  
  5.    extend FFI::Library
  6.  
  7.    ffi_lib "C:/Program Files/Sketchup/Sketchup 2017/SketchupAPI.dll"
  8.  
  9.    enum :SUResult, [
  10.       :SU_ERROR_NONE,
  11.       :SU_ERROR_NULL_POINTER_INPUT,
  12.       :SU_ERROR_INVALID_INPUT,
  13.       :SU_ERROR_NULL_POINTER_OUTPUT,
  14.       :SU_ERROR_INVALID_OUTPUT,
  15.       :SU_ERROR_OVERWRITE_VALID,
  16.       :SU_ERROR_GENERIC,
  17.       :SU_ERROR_SERIALIZATION,
  18.       :SU_ERROR_OUT_OF_RANGE,
  19.       :SU_ERROR_NO_DATA,
  20.       :SU_ERROR_INSUFFICIENT_SIZE,
  21.       :SU_ERROR_UNKNOWN_EXCEPTION,
  22.       :SU_ERROR_MODEL_INVALID,
  23.       :SU_ERROR_MODEL_VERSION,
  24.       :SU_ERROR_LAYER_LOCKED,
  25.       :SU_ERROR_DUPLICATE,
  26.       :SU_ERROR_PARTIAL_SUCCESS,
  27.       :SU_ERROR_UNSUPPORTED,
  28.       :SU_ERROR_INVALID_ARGUMENT,
  29.       :SU_ERROR_ENTITY_LOCKED,
  30.       :SU_ERROR_INVALID_OPERATION
  31.    ]
  32.  
  33.    enum :SUModelVersion, [
  34.       :SUModelVersion_SU3,
  35.       :SUModelVersion_SU4,
  36.       :SUModelVersion_SU5,
  37.       :SUModelVersion_SU6,
  38.       :SUModelVersion_SU7,
  39.       :SUModelVersion_SU8,
  40.       :SUModelVersion_SU2013,
  41.       :SUModelVersion_SU2014,
  42.       :SUModelVersion_SU2015,
  43.       :SUModelVersion_SU2016,
  44.       :SUModelVersion_SU2017,
  45.       :SUModelVersion_SU2018,
  46.       :SUModelVersion_SU2019
  47.    ]
  48.  
  49.    class SURef < FFI::Struct
  50.       layout :ptr, :pointer
  51.    end
  52.  
  53.    class SUPoint3d < FFI::Struct
  54.       layout :x, :double,
  55.          :y, :double,
  56.          :z, :double
  57.    end
  58.  
  59.    # SU_RESULT SUModelCreate(SUModelRef* model);
  60.    attach_function(:create_model, :SUModelCreate, [:pointer], :SUResult)
  61.  
  62.    # SU_RESULT SUModelSaveToFileWithVersion(SUModelRef model, const char* file_path, enum SUModelVersion version);
  63.    attach_function(:save_to_file_with_version, :SUModelSaveToFileWithVersion, [SURef, :string, :SUModelVersion], :SUResult)
  64.  
  65.    # SU_RESULT SUModelGetEntities(SUModelRef model, SUEntitiesRef* entities);
  66.    attach_function(:get_entities, :SUModelGetEntities, [SURef, SURef], :SUResult)
  67.  
  68.    # SU_RESULT SUGuidePointCreate(SUGuidePointRef* guide_point, const struct SUPoint3D* position);
  69.    attach_function(:create_guide_point, :SUGuidePointCreate, [:pointer, :pointer], :SUResult)
  70.  
  71.    # SU_RESULT SUEntitiesAddGuidePoints(SUEntitiesRef entities, size_t len, const SUGuidePointRef guide_points[]);
  72.    attach_function(:add_guide_point, :SUEntitiesAddGuidePoints, [SURef, :size_t, :pointer], :SUResult)
  73.  
  74.  
  75. end # module SketchupFFI
  76.  
  77.  
  78. include SketchupFFI
  79.  
  80. # Create a Model
  81. model = SURef.new
  82. res = create_model model
  83. if res != :SU_ERROR_NONE
  84.    puts "Could not create model: #{res}"
  85.    exit
  86. end
  87.  
  88. # Get the Model's Entities
  89. ents = SURef.new
  90. res = get_entities(model, ents)
  91. if res != :SU_ERROR_NONE
  92.    puts "Could not get entities: #{res}"
  93.    exit
  94. end
  95.  
  96. # Need to create an array of SUGuidePointRef,
  97. # or in this code just an array of SURef since it's the same thing.
  98. len = 3
  99. size = SURef.size
  100. points = FFI::MemoryPointer.new(SURef, len)
  101. len.times {|i|
  102.    ptr = points + (i * size)
  103.    pt3d = SUPoint3d.new
  104.    pt3d[:x] = rand(10); pt3d[:y] = rand(10); pt3d[:z] = rand(10)
  105.    res = create_guide_point(ptr, pt3d)
  106.    if res != :SU_ERROR_NONE
  107.       puts "Could not create GuidePoint: #{res}"
  108.       exit
  109.    end
  110. }
  111.  
  112. # res == SU_ERROR_INVALID_INPUT: An API object input to the function was not created properly.
  113. # or a SegFault
  114. res = add_guide_point(ents[:ptr], len*SURef.size, points)
  115. if res != :SU_ERROR_NONE
  116.    puts "Could not add GuidePoint to Entities: #{res}"
  117.    exit
  118. end
  119.  
  120. save_to_file_with_version(model[:ptr], "model.skp", :SUModelVersion_SU2017)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top