Page 1 sur 1

TUTO draw in mm with grid and zoom

Publié : lun. 18/mars/2019 14:04
par microdevweb
Bonjour à tous,
Je travaille actuellement sur un logiciel de plan (2d -> 3d) en Pb, je met à votre disposition ce petit bout de code pour la gestion d'un dessin en mm avec grille magnétique et zoom
  • CTRL + molette pour augmenté ou diminué le zoom


Image

Image

Code : Tout sélectionner

; ********************************************************************************************************
; AUTHOR          : MICRODEVWEB
; PROJECT         : TUTO DRAW
; DESIGNED WITH   : PB 5.70
; ********************************************************************************************************
EnableExplicit
Enumeration 
  #MAIN_FORM
  #CANVAS
EndEnumeration
Structure pos
  x.l
  y.l
  w.l
  h.l
EndStructure
; constantes
#BACK_COLOR = $FFAAE8EE
#GRID_COLOR = $8213458B
#CURSOR_COLOR = $F70000FF
#GRID_SIZE = 50 ; mm
#GRID_SENSITIVE = 20.0
#BOX_WIDTH = 1000 ; mm
#BOX_HEIGHT = 100 ; mm
; Variables globales
Global gZoom.f = 0.2 ; facteur de zoom
Global gMX,gMY ; position de la souris

Procedure coordinatesTo_MM(x,y,*x,*y)
  PokeL(*x,ConvertCoordinateX(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  PokeL(*Y,ConvertCoordinateY(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
EndProcedure
Procedure coordinatesTo_MM_bis(x,y,*x,*y)
  StartVectorDrawing(CanvasVectorOutput(#CANVAS,#PB_Unit_Millimeter))
  ScaleCoordinates(gZoom,gZoom) ; le facteur de zoom
  PokeL(*x,ConvertCoordinateX(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  PokeL(*Y,ConvertCoordinateY(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  StopVectorDrawing()
EndProcedure

Procedure drawGrid()
  ; dessin de la grille
  Protected r = #GRID_SIZE,c = #GRID_SIZE
  Protected w,h 
  ; on transform la taille du canvas en mm
  coordinatesTo_MM(GadgetWidth(#CANVAS),GadgetHeight(#CANVAS),@w,@h)
  ; on efface
  VectorSourceColor(#BACK_COLOR)
  FillVectorOutput()
  ; dessin des colonnes
  While c < w
    MovePathCursor(c,0)
    AddPathLine(c,h)
    c + #GRID_SIZE
  Wend
  ; dessin des lignes
  While r < h
    MovePathCursor(0,r)
    AddPathLine(w,r)
    r + #GRID_SIZE
  Wend
  VectorSourceColor(#GRID_COLOR)
  StrokePath(0.2) ; Attention en mm
EndProcedure

Procedure drawCursor()
  Protected w,h 
  ; on transform la taille du canvas en mm
  coordinatesTo_MM(GadgetWidth(#CANVAS),GadgetHeight(#CANVAS),@w,@h)
  ; horizontal
  MovePathCursor(0,gMY)
  AddPathLine(w,gMY)
  ; vertical
  MovePathCursor(gMX,0)
  AddPathLine(gMx,h)
  VectorSourceColor(#CURSOR_COLOR)
  StrokePath(0.3) ; Attention en mm
EndProcedure

Procedure manageMagnet()
  ; gestion de l'aimantation
  Protected x,y,lM.f = #GRID_SIZE  - #GRID_SENSITIVE,LP.f = #GRID_SENSITIVE
    If Mod(gMX,#GRID_SIZE) >= LM
      x =  Round(gMX/#GRID_SIZE,#PB_Round_Up) * #GRID_SIZE
      gMX = x
    ElseIf Mod(gMX,#GRID_SIZE) <= LP
      x =  Round(gMX/#GRID_SIZE,#PB_Round_Nearest) * #GRID_SIZE
      gMX = x
    EndIf
    If Mod(gMY,#GRID_SIZE) >= LM
      Y =  Round(gMY/#GRID_SIZE,#PB_Round_Up) * #GRID_SIZE
      gMY = y
    ElseIf Mod(gMY,#GRID_SIZE) <= LP
      Y =  Round(gMY/#GRID_SIZE,#PB_Round_Nearest) * #GRID_SIZE
      gMY = y
    EndIf
EndProcedure

Procedure draw()
  ; dessin du canvas en mm
  StartVectorDrawing(CanvasVectorOutput(#CANVAS,#PB_Unit_Millimeter))
  ScaleCoordinates(gZoom,gZoom) ; le facteur de zoom
  drawGrid()                    ; dessin de la grille
  drawCursor()                  ; dessin du curseur
  StopVectorDrawing()
EndProcedure

Procedure evExit()
  End
EndProcedure

Procedure evCanvas()
  Select EventType()
    Case #PB_EventType_MouseWheel
      ; gestion du zoom uniquement si touche controle appuyée
      If GetGadgetAttribute(#CANVAS,#PB_Canvas_Modifiers) = #PB_Canvas_Control
        If GetGadgetAttribute(#CANVAS,#PB_Canvas_WheelDelta) < 1
          If gZoom > 0.03
            gZoom -0.01
          EndIf
        Else
          If gZoom <100
            gZoom +0.01
          EndIf
        EndIf
      EndIf
      draw()
    Case #PB_EventType_MouseMove
      ; on mémorise la position de la souris
      coordinatesTo_MM_bis(GetGadgetAttribute(#CANVAS,#PB_Canvas_MouseX),
                           GetGadgetAttribute(#CANVAS,#PB_Canvas_MouseY),
                           @gMX,@gMY)
      manageMagnet()
      draw()
  EndSelect
EndProcedure

Procedure openForm()
  Protected flags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu
  OpenWindow(#MAIN_FORM,0,0,800,600,"Tuto draw in mm",flags)
  CanvasGadget(#CANVAS,0,0,800,600,#PB_Canvas_Keyboard) ; important pour lire le delata wheel
  ; mise en place des callback
  BindEvent(#PB_Event_CloseWindow,@evExit(),#MAIN_FORM)
  BindGadgetEvent(#CANVAS,@evCanvas())
  
  draw()
EndProcedure

openForm()
; main loop
Repeat : WaitWindowEvent() : ForEver

Re: TUTO draw in mm with grid and zoom

Publié : lun. 18/mars/2019 16:01
par Micoute
Bonjour microdevweb et merci beaucoup pour le partage.

Re: TUTO draw in mm with grid and zoom

Publié : mar. 19/mars/2019 19:53
par Kwai chang caine
M'étonnera toujours ce PB du minimum de lignes pour un max d'effets 8O
Merci pour le partage 8)

Re: TUTO draw in mm with grid and zoom

Publié : mar. 19/mars/2019 21:37
par SPH
Kwai chang caine a écrit :M'étonnera toujours ce PB du minimum de lignes pour un max d'effets 8O
Merci pour le partage 8)
+1 8) 8O

Re: TUTO draw in mm with grid and zoom

Publié : mer. 20/mars/2019 5:14
par Ollivier
Et encore, la procédure magnet peut être encore considérablement diminuée.

Re: TUTO draw in mm with grid and zoom

Publié : mer. 20/mars/2019 8:56
par Kwai chang caine
En tout cas pas par moi :oops: :wink:

Re: TUTO draw in mm with grid and zoom

Publié : mar. 18/juin/2019 15:58
par Philippe_GEORGES
Impressionnant !

Là, je me dit qu'il y a des têtes sur ce forum !

Un grand merci pour le partage.

Re: TUTO draw in mm with grid and zoom

Publié : mar. 18/juin/2019 21:09
par Ar-S
Très propre l'interface.
Bonne continuation. C'est du lourd.