https://foro.gambas-es.org/viewtopic.php?f=5&t=5100&p=29766#p29766
-----------------------------------
vuott
Jueves, 21 Enero 2016, 14:15

Re: LibreDWG En Gambas
-----------------------------------
[quote user="tincho" post="29716"]Luego, también instale la librería [b]pslib[/b][/quote]
tincho me pidió hacer disponible para Gambas este programa
  https://github.com/ArchimedesCAD/libredwg/blob/master/examples/dwg_ps.c

Yo he escrito una aplicación con recursos Gambas, funciones externas y un po' de puro C.
Hace falta instalar en el sistema la libreria [i]pslib[/i].
[code linenumbers=false syntax=gambas]Private Const H As Integer = 2000


Library "libredwg:0.0.0"

' int dwg_read_file(char *filename, Dwg_Data * dwg)
' Open filename and decode it, saving information into dwg.
Private Extern dwg_read_file(filename As String, dwg As Pointer) As Integer

' double dwg_model_x_min(Dwg_Data *)
Private Extern dwg_model_x_min(Dwg_Data As Pointer) As Float

' double dwg_model_x_max(Dwg_Data *)
Private Extern dwg_model_x_max(Dwg_Data As Pointer) As Float

' double dwg_model_y_min(Dwg_Data *)
Private Extern dwg_model_y_min(Dwg_Data As Pointer) As Float

' double dwg_model_y_max(Dwg_Data *)
Private Extern dwg_model_y_max(Dwg_Data As Pointer) As Float


Library "libps:0.4.5"

' void PS_boot(void)
' Initialize library.
Private Extern PS_boot()

' PSDoc * PS_new(void)
' Creating a new PostScript document object.
Private Extern PS_new() As Pointer

' int PS_open_file(PSDoc *psdoc, const char *filename)
' Opens a file for output.
Private Extern PS_open_file(psdoc As Pointer, filename As String) As Integer

' void PS_set_info(PSDoc *p, const char *key, const char *val)
' Sets information fields of document.
Private Extern PS_set_info(p As Pointer, key As String, valS As String)

' void PS_begin_page(PSDoc *psdoc, float width, float height)
' Start a new page.
Private Extern PS_begin_page(psdoc As Pointer, width As Single, height As Single)

' void PS_scale(PSDoc *psdoc, float x, float y)
' Sets scaling factor.
Private Extern PS_scale(psdoc As Pointer, x As Single, y As Single)

' void PS_translate(PSDoc *psdoc, float x, float y)
' Sets translation.
Private Extern PS_translate(psdoc As Pointer, x As Single, y As Single)

' void PS_circle(PSDoc *psdoc, float x, float y, float radius)
' Draws a circle.
Private Extern PS_circle(psdoc As Pointer, x As Single, y As Single, radius As Single)

' void PS_moveto(PSDoc *psdoc, float x, float y)
' Sets current point.
Private Extern PS_moveto(psdoc As Pointer, x As Single, y As Single)

' void PS_lineto(PSDoc *psdoc, float x, float y)
' Draws a line.
Private Extern PS_lineto(psdoc As Pointer, x As Single, y As Single)

' void PS_stroke(PSDoc *psdoc)
' Draws the current path.
Private Extern PS_stroke(psdoc As Pointer)

' void PS_end_page(PSDoc *psdoc)
' End a page.
Private Extern PS_end_page(psdoc As Pointer)

' void PS_close(PSDoc *psdoc)
' Closes a PostScript document.
Private Extern PS_close(psdoc As Pointer)

' void PS_delete(PSDoc *psdoc)
' Deletes all resources of a PostScript document.
Private Extern PS_delete(psdoc As Pointer)

' void PS_shutdown(void)
' Final clean up of library.
Private Extern PS_shutdown()


Library "/tmp/libadhoc"

' int Dim_dwg()
' Obtiene la dimensión de la Estructura '_dwg_struct'.
Private Extern Dim_dwg() As Integer

' void Itera_entidades(struct _dwg_struct *dwg)
' Iterar todas las entidades.
Private Extern Itera_entidades(_dwg As Pointer)


Public Sub Main()

  Dim i, rit As Integer
  Dim filename_in, filename_out As String
  Dim p_dwg As Pointer
  
    CreaSo()
  
' Obtiene la dimensión de la Estructura '_dwg_struct' y asigna memoria para el Puntero a la Estructura:
    i = Dim_dwg()
    If i = 0 Then Error.Raise("Error al obtener la dimensión !")
    p_dwg = Alloc(i)
    
    filename_in = "/ruta/del/archivo.dwg"
    
' Lee el archivo .dwg:
    rit = dwg_read_file(filename_in, p_dwg)
    If rit Then
      Error.Raise("Imposible leer el archivo " & filename_in & " !")
    Else
      Print "¡La lectura del archivo ha tenido éxito!"
    Endif
    
    filename_out = File.BaseName(filename_in) & ".ps"
    
    Crea_Postscript(p_dwg, filename_out)

' Libera la memoria:
    Free(p_dwg)

End


Private Procedure Crea_Postscript(dwg As Pointer, exitus As String)
  
  Dim dx, dy, scale_x, scale_y, scale As Single
  Dim rit As Integer
  Dim ps As Pointer
  
' Inizialización de la libreria ps:
    PS_boot()
    ps = PS_new()
    
    rit = PS_open_file(ps, exitus)
    If rit < 0 Then Error.Raise("Imposible escribir un archivo PostScript !")
    
    PS_set_info(ps, "Creator", "dwg_ps")
    PS_set_info(ps, "Author", "LibreDWG example")
    PS_set_info(ps, "Title", "DWG to Postscript example")
    PS_set_info(ps, "Keywords", "dwg, postscript, conversion, CAD, plot")
    
    dx = (dwg_model_x_max(dwg) - dwg_model_x_min(dwg))
    dy = (dwg_model_y_max(dwg) - dwg_model_y_min(dwg))
    scale_x = dx / (dwg_model_x_max(dwg) - dwg_model_x_min(dwg))
    scale_y = dy / (dwg_model_y_max(dwg) - dwg_model_y_min(dwg))
    scale = 25.4 / 72                                            ' pt: mm
    PS_begin_page(ps, dx / scale, dy / scale)
    scale *= IIf(scale_x > scale_y, scale_x, scale_y)
    PS_scale(ps, scale, scale)
    PS_translate(ps, - dwg_model_x_min(dwg), - dwg_model_y_min(dwg))
  
    PS_circle(ps, 0, 0, H)
    PS_moveto(ps, 0, H)
    PS_lineto(ps, 0, - H)
    PS_moveto(ps, - H, 0)
    PS_lineto(ps, H, 0)
    PS_stroke(ps)
    
    Itera_entidades(dwg, ps)
    
' Terminando:
    PS_end_page(ps)
    PS_close(ps)
    PS_delete(ps)
    PS_shutdown()
    
End


Private Procedure CreaSo()
  
  File.Save("/tmp/libadhoc.c", "#include <stdio.h>\n" &
            "#include <dwg.h>\n" &
            "#include <libps/pslib.h>" &
            "\n\n" &
          "int Dim_dwg() {\n" &
            "   return sizeof(struct _dwg_struct);\n}" &
            "\n\n" &
          "void Itera_entidades(struct _dwg_struct *dwg, PSDoc *ps){\n" &
            "   Dwg_Object *obj;\n" &
            "   int i;\n" &
            "   for (i = 0; i < dwg->num_objects; i++) {\n" &
            "     obj = &dwg->object[i];\n" &
            "     if (obj->supertype = DWG_SUPERTYPE_UNKNOWN)\n" &
            "       continue;\n" &
            "     if (obj->type = DWG_SUPERTYPE_OBJECT)\n" &
            "       continue;\n" &
            "     if (obj->tio.entity->entity_mode == 0)\n" &
            "       continue;\n" &
            "     if (obj->type == DWG_TYPE_LINE) {\n" &
            "       Dwg_Entity_LINE* line;\n" &
            "       line = obj->tio.entity->tio.LINE;\n" &
            "       PS_moveto(ps, line->start.x, line->start.y);\n" &
            "       PS_lineto(ps, line->end.x, line->end.y);\n" &
            "       PS_stroke(ps);\n     }\n   }\n}")

  Shell "gcc -o /tmp/libadhoc.so /tmp/libadhoc.c -shared -lredwg -lps" Wait 
  
End[/code]


