Inserire un rettangolo personalizzato nella diapositiva corrente

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/Impress/001 and the translation is 100% complete.


    Other languages:

    Descrizione

    Questa macro inserire un rettangolo com.sun.star.drawing.RectangleShape[1] nella diapositiva corrente in un documento di Impress, con varie impostazioni predefinite specificate nel codice.

    Codice

    LibreOffice Basic

    Option Explicit
    
    Sub InsertRectangleShape()
        '''Inserisce un rettangolo RectangleShape nella diapositiva corrente'''
    
        Dim oDoc As Object ' com.sun.star.lang.XComponent
        Dim oSlide As Object ' com.sun.star.drawing.XDrawPage
        Dim oRectangle As Object ' com.sun.star.drawing.RectangleShape
        Dim oSize As New com.sun.star.awt.Size
        Dim oPosition As New com.sun.star.awt.Point
    
        oDoc = ThisComponent
        oSlide = oDoc.CurrentController.CurrentPage
        oRectangle = oDoc.createInstance("com.sun.star.drawing.RectangleShape")
    
        With oSlide
            .add(oRectangle)
            oPosition.X = .7*(.Width - oSize.Width)
            oPosition.Y = .7*(.Height - oSize.Height)
        End With ' oSlide
    
        oSize.Width = 5000 ' 50 mm
        oSize.Height = 3000 ' 30 mm
        With oRectangle
            .setSize(oSize)
            .setPosition(oPosition)
            .FillColor = RGB(0, 0, 255) ' BLU
            .FillStyle = com.sun.star.drawing.FillStyle.SOLID ' o .NONE
            .FillTransparence = 30 ' 0-100 da opaco a trasparente
    
            .LineStyle = com.sun.star.drawing.LineStyle.SOLID ' o .NONE
            .LineWidth = 100 ' 1 mm
            .LineColor = RGB(0, 0, 0) ' NERO
    
            .setName("MyBasicRectangle")
            .setString("Hello from Basic!")
            .CharFontName = "Liberation Sans"
            .CharColor = RGB(255, 255, 255) ' BIANCO
            .ZOrder = 1
        End With ' oRectangle
    
    End Sub ' InsertRectangleShape


    Python

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from com.sun.star.awt import Size
    from com.sun.star.awt import Point
    from com.sun.star.drawing.FillStyle import SOLID as SOLID_FILLSTYLE
    from com.sun.star.drawing.LineStyle import SOLID as SOLID_LINESTYLE
    
    _GREEN, _BLACK, _RED = 0x00ff00, 0x000000, 0xff0000
    _VALUES = range(0,256,1)  # 0-255 valori interi
    
    def _RGB(red: "# come 0-255", green: int, blue: "valori da un byte") -> int:
        """Restituisce il valore numerico di un colore, formato dai componenti rosso, verde e blu"""
    
        if not (red in _VALUES and green in _VALUES and blue in _VALUES):
            raise ValueError
        return (red * 2**16) + (green * 2**8) + (blue * 2**0)
    
        # nota: gli argomenti ''rosso'', ''verde'' e ''blu'' della funzione RGB sono valori interi da un byte senza segno.
    
    def InsertRectangleShape():
        """Inserisce un rettangolo (RectangleShape) nella diapositiva corrente"""
    
        desktop = XSCRIPTCONTEXT.getDesktop()
        model = desktop.getCurrentComponent()
        slide = model.CurrentController.CurrentPage    
        rect = model.createInstance("com.sun.star.drawing.RectangleShape")
        slide.add(rect)  # Crea il rettangolo
    
        size = Size()
        size.Width = 5000  # 50 mm
        size.Height = 3000  # 50 mm
        
        position = Point()
        position.X = 0.9*(slide.Width - size.Width)
        position.Y = 0.5*(slide.Width - size.Width)
        
        rect.setSize(size)
        rect.setPosition(position)
    
        rect.FillStyle = SOLID_FILLSTYLE  # o .NONE
        rect.FillColor = _RGB(255, 0, 0)  # _RED
        rect.FillTransparence = 30  # 0-100 da opaco a trasparente
        rect.LineStyle = SOLID_LINESTYLE  # o .NONE
        rect.LineWidth = 100  # 1 mm
        rect.LineColor = _RGB(0, 0, 0)  # _BLACK
        rect.setName("MyPythonRectangle")
        rect.setString("Hello from Python!")
        rect.CharFontName ='Lucida Calligraphy'
        rect.CharColor = _RGB(0, 255, 0)  # _GREEN
        rect.ZOrder = 1
    
        
    g_exportedScripts = (InsertRectangleShape,)

    File ODP per eseguire la macro

    Note