Jump to content

Insertar una forma rectangular en Impress

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


Descripción

Esta macro inserta un com.sun.star.drawing.RectangleShape[1] en la diapositiva seleccionada en un documento de Impress , con varios ajustes predefinidos especificados en el código.

Código

LibreOffice Basic

Option Explicit

Sub InsertRectangleShape()
    '''Inserta una forma rectangular en la diapositiva seleccionada'''

    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) ' AZUL
        .FillStyle = com.sun.star.drawing.FillStyle.SOLID ' o .NONE
        .FillTransparence = 30 ' de 0 a 100 = Opaco > transparente

        .LineStyle = com.sun.star.drawing.LineStyle.SOLID ' o .NONE
        .LineWidth = 100 ' 1 mm
        .LineColor = RGB(0, 0, 0) ' NEGRO

        .setName("MyBasicRectangle")
        .setString("Hello from Basic!")
        .CharFontName = "Liberation Sans"
        .CharColor = RGB(255, 255, 255) ' BLANCO
        .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 a 255 valores en enteros

def _RGB(red: "# de 0-255", green: int, blue: "valores de un byte") -> int:
    """Devuelve el número equivalente al color en componentes rojo, verde y azul"""

    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: los argumentos ''red'', ''green'' y ''blue'' de la función RGB son valores de enteros sin signo

def InsertRectangleShape():
    """Inserta una forma rectangular en la diapositiva seleccionada"""

    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    slide = model.CurrentController.CurrentPage    
    rect = model.createInstance("com.sun.star.drawing.RectangleShape")
    slide.add(rect)  # Crear el rectángulo

    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  # de 0 a 100 = Opaco > transparente
    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,)

Archivo ODP para ejecutar la macro

Notas: