Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'ffi'
- module SketchupFFI
- extend FFI::Library
- ffi_lib "C:/Program Files/Sketchup/Sketchup 2017/SketchupAPI.dll"
- enum :SUResult, [
- :SU_ERROR_NONE,
- :SU_ERROR_NULL_POINTER_INPUT,
- :SU_ERROR_INVALID_INPUT,
- :SU_ERROR_NULL_POINTER_OUTPUT,
- :SU_ERROR_INVALID_OUTPUT,
- :SU_ERROR_OVERWRITE_VALID,
- :SU_ERROR_GENERIC,
- :SU_ERROR_SERIALIZATION,
- :SU_ERROR_OUT_OF_RANGE,
- :SU_ERROR_NO_DATA,
- :SU_ERROR_INSUFFICIENT_SIZE,
- :SU_ERROR_UNKNOWN_EXCEPTION,
- :SU_ERROR_MODEL_INVALID,
- :SU_ERROR_MODEL_VERSION,
- :SU_ERROR_LAYER_LOCKED,
- :SU_ERROR_DUPLICATE,
- :SU_ERROR_PARTIAL_SUCCESS,
- :SU_ERROR_UNSUPPORTED,
- :SU_ERROR_INVALID_ARGUMENT,
- :SU_ERROR_ENTITY_LOCKED,
- :SU_ERROR_INVALID_OPERATION
- ]
- enum :SUModelVersion, [
- :SUModelVersion_SU3,
- :SUModelVersion_SU4,
- :SUModelVersion_SU5,
- :SUModelVersion_SU6,
- :SUModelVersion_SU7,
- :SUModelVersion_SU8,
- :SUModelVersion_SU2013,
- :SUModelVersion_SU2014,
- :SUModelVersion_SU2015,
- :SUModelVersion_SU2016,
- :SUModelVersion_SU2017,
- :SUModelVersion_SU2018,
- :SUModelVersion_SU2019
- ]
- class SURef < FFI::Struct
- layout :ptr, :pointer
- end
- class SUPoint3d < FFI::Struct
- layout :x, :double,
- :y, :double,
- :z, :double
- end
- # SU_RESULT SUModelCreate(SUModelRef* model);
- attach_function(:create_model, :SUModelCreate, [:pointer], :SUResult)
- # SU_RESULT SUModelSaveToFileWithVersion(SUModelRef model, const char* file_path, enum SUModelVersion version);
- attach_function(:save_to_file_with_version, :SUModelSaveToFileWithVersion, [SURef, :string, :SUModelVersion], :SUResult)
- # SU_RESULT SUModelGetEntities(SUModelRef model, SUEntitiesRef* entities);
- attach_function(:get_entities, :SUModelGetEntities, [SURef, SURef], :SUResult)
- # SU_RESULT SUGuidePointCreate(SUGuidePointRef* guide_point, const struct SUPoint3D* position);
- attach_function(:create_guide_point, :SUGuidePointCreate, [:pointer, :pointer], :SUResult)
- # SU_RESULT SUEntitiesAddGuidePoints(SUEntitiesRef entities, size_t len, const SUGuidePointRef guide_points[]);
- attach_function(:add_guide_point, :SUEntitiesAddGuidePoints, [SURef, :size_t, :pointer], :SUResult)
- end # module SketchupFFI
- include SketchupFFI
- # Create a Model
- model = SURef.new
- res = create_model model
- if res != :SU_ERROR_NONE
- puts "Could not create model: #{res}"
- exit
- end
- # Get the Model's Entities
- ents = SURef.new
- res = get_entities(model, ents)
- if res != :SU_ERROR_NONE
- puts "Could not get entities: #{res}"
- exit
- end
- # Need to create an array of SUGuidePointRef,
- # or in this code just an array of SURef since it's the same thing.
- len = 3
- size = SURef.size
- points = FFI::MemoryPointer.new(SURef, len)
- len.times {|i|
- ptr = points + (i * size)
- pt3d = SUPoint3d.new
- pt3d[:x] = rand(10); pt3d[:y] = rand(10); pt3d[:z] = rand(10)
- res = create_guide_point(ptr, pt3d)
- if res != :SU_ERROR_NONE
- puts "Could not create GuidePoint: #{res}"
- exit
- end
- }
- # res == SU_ERROR_INVALID_INPUT: An API object input to the function was not created properly.
- # or a SegFault
- res = add_guide_point(ents[:ptr], len*SURef.size, points)
- if res != :SU_ERROR_NONE
- puts "Could not add GuidePoint to Entities: #{res}"
- exit
- end
- save_to_file_with_version(model[:ptr], "model.skp", :SUModelVersion_SU2017)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement