| View previous topic :: View next topic |
| Author |
Message |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Fri Nov 06, 2009 4:12 pm Post subject: Change Picture for OpenOffcie (BASIC) |
|
|
Change Picture is a macro I made for OpenOffice that lets you change the picture of an existing image in a document.
http://www.youtube.com/watch?v=8xZnpFnAAQQ
http://extensions.services.openoffice.org/project/ChangePicture
Este es una macro que hice para OpenOffice que te permite cambiar la fotografiá de un imagen existente en un documento.
I want to thank james_h, if he wouldn't had encourage me to keep working on it I wouldn't have made the extension. Thanks man, I really appreciate your support and advising I'm really in debt with you.
I want to thank every body in this forum, since I was in a rush I didn't take note of all the post I had to go trough. I'll say there were more than 50 (I'm not a quick learner) and most of the time it was copy and paste and trying to understand the code. Thank you all.
Also I need to thank Paolo Mantovani for his BasicAddonBuilder - The Extension's packager is a great tool:
http://extensions.services.openoffice.org/project/BasicAddonBuilder
Hope you find this macro use full. Talking about that, it hasn't pass a day and I already have 284 downloads on my extension. This is the first time I get so mush attention in the Internet. Thank you all.
Last edited by ArielEnter on Sat Apr 30, 2011 9:35 am; edited 20 times in total |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Fri Nov 06, 2009 5:46 pm Post subject: commented |
|
|
Deleted
This is the code
| Code: | REM ***** BASIC *****
sub Entire
' * Most of the code is explained in the variables declarations below
Dim oDoc As Object ' Working document
Dim oElements As Object ' All elements that are currently selected by the user
Dim oFirst As Object ' Is the first element of all currently selected elements that may or may not be an image
Dim oAllFiles As Object ' All files' url that were selected in the file picker dialog
Dim oFirstFile ' The first file's url of all the files selected in the file picker dialog
Dim oDialog As Object ' File picker dialog
Dim oBitmaps as object ' holds the pictures embedded to the document
oDoc = thisComponent '*
Dim s1$ ' Graphic service name
Dim s2$ ' Graphic service name
On Error Goto ErrorHandler
oElements = oDoc.getCurrentSelection() '*
oFirst = oElements(0) '*
s1 = "com.sun.star.drawing.GraphicObjectShape"
s2 = "com.sun.star.text.TextGraphicObject"
If not(oFirst.supportsService(s1) OR oFirst.supportsService(s2)) Then ' Checks if oFirst is not an image
msgbox( NO_IMAGE_IS_SELECTED & " " & UNABLE_TO_REPLACE_IMAGE ,64, CHANGE_PICTURE) 'If is not an image the program will stop
exit sub
else
oImg = oFirst 'In the other hand, if it is a image, we will intent to change its picture
end if
oDialog = createUnoService("com.sun.star.ui.dialogs.FilePicker") ' *
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_LINK_PREVIEW) 'Specify what kind of dialog it will be. In our case is a file picker.
oDialog.Initialize(sFilePickerArgs()) ' We initialize the dialog as a FILEOPEN_LINK_PREVIEW
oDialog.AppendFilter( "All Image Formats (*.jpeg;*.jpg;*.gif;*.png;*.tiff;*.tif;*.bmp;...", "*.jpeg;*.jpg;*.gif;*.png;*.tiff;*.tif;*.bmp;*.dxf;*.emf;*.eps;*.met;*.pbm;*.pcd;*.pct,*.psx;*.pgm;*.ppm;*.psd;*.ras;*.sgf;*.ras;*.sgf;*.sgv;*.svm;*.tga;*wmf;*.xbm;*.xpm") ' We tell the dialog to display only image formats to chose from
if oDialog.execute = 0 then ' Will display the dialog and if the user decided to close the dialog instead of chosen a file it will return 0 in which case it'll end up the program
exit sub
end if
oAllFiles() = oDialog.getFiles() '*
oFirstFile = oAllFiles(0) '*
if oDialog.getValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 0) = True then 'If the user chooses to link the file instead of embed it, the check box will be true
oImg.GraphicURL = oFirstFile ' gives to the image the hard drive location of the file and ends up the program
'oDoc.refresh() it didn't work in Cal, I tried in a couple of computers and it wasn't needed anymore
exit sub
end if
oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" ) '*
oBitmaps.insertByName("Image", oFirstFile) ' we store the image in the bitmap holder
oImg.GraphicURL = oBitmaps.getByName("Image") ' we give the image the location of the picture which is stored within the document
oBitmaps.removeByName("Image") 'once that we have given the image a picture we no longer need it inside of the oBitmap
'oDoc.refresh() 'Seems not needed anymore
exit sub
ErrorHandler:
msgbox( NO_IMAGE_IS_SELECTED & " " & UNABLE_TO_REPLACE_IMAGE ,48, CHANGE_PICTURE)
End Sub |
This code is over GPL 3 or later.
Last edited by ArielEnter on Mon Aug 30, 2010 11:48 am; edited 2 times in total |
|
| Back to top |
|
 |
james_h Super User


Joined: 05 Nov 2005 Posts: 883
|
Posted: Fri Nov 06, 2009 9:37 pm Post subject: Test if selection supports Service |
|
|
| Quote: | | One of the things I wish I could have changed is getting a better way to find out if the selected object is an image or not. |
Your code might be able to use the thisComponent.getCurrentSelection.supportsService("NameOfService") test to determine what has been selected. I haven't tested this snippet extensively, but you should get an idea from it.
| Code: | Sub testGraph
oDocument = thisComponent
oSelection = oDocument.getCurrentSelection()
if oSelection.supportsService("com.sun.star.text.TextRanges") then
MsgBox ("Selection is text")
else
MsgBox ("Selection is image")
end if
end sub | As far as I know, TextRanges are only applicable to writer documents. Also, any objects that are not text (for example, Drawing Objects, Embedded OLE Objects) are rated as images according to this snippet. What I have not yet determined is the name of a service that is unique to inserted jpeg, png, tif and gif images.
This looks like an interesting utility. When you've fine tuned it, maybe you should package it as an extension. |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Fri Nov 20, 2009 3:55 pm Post subject: |
|
|
One thing I was having troubles with was the concept of a Bitmap table. I haven't read about the matter, and I never got a lesson in school about that, so I may be wrong. For me, a bitmap table is variant that holds the picture itself in the document. So in order to embed an image to the document, instead of referring to a location in the file system of the OS (the hard drive location) you tell the image object to take the picture from the bit map table located inside the document it self.
While making the program, I was afraid of leaving in the bit map table images that were no longer necessary every time Picture changed, making the document unnecessarily bigger and bigger.
According to my personal conception of a bitmap table, if you delete a picture of the bitmap table, all images using that same picture will lost the ability to display that picture. But, by testing it my self, and as my Change Picture Extension shows, this is not true.
| Code: | oImg.GraphicURL = oBitmaps.getByName("Image") ' we give the image the location of the picture which is stored within the document
oBitmaps.removeByName("Image") 'once that we have given the image a picture we no longer need it inside of the oBitmap
|
The only thing I can think of is that the Image object is capable of holding the picture itself. Now this is good, because it means a more efficient control over the size of the document.
If the image object is deleted, the picture is deleted too and no longer exist inside of the document. This will mean that the size of the document will decrease as is logical. And since all entries in the bitmap table are deleted right after they are used too, the bitmap table doesn't take any space in the document either.
Please, could someone tell me if I'm right in my concepts? I'll really appreciate it.
I have tested and the program works very well, and pictures are not lost when you move them to another computer if they were embedded and even if the bitmap talble is empty.
Last edited by ArielEnter on Mon Aug 30, 2010 11:43 am; edited 1 time in total |
|
| Back to top |
|
 |
james_h Super User


Joined: 05 Nov 2005 Posts: 883
|
Posted: Mon Nov 23, 2009 6:15 pm Post subject: Image - How to determine whether it is linked or embedded |
|
|
An image in an OpenOffice.org document can be either embedded in the document itself or a link to an external image. The OpenOffice.org help says:
| Quote: | Inserting Bitmaps
A bitmap image can be inserted in OpenOffice.org Writer, OpenOffice.org Calc, OpenOffice.org Draw and OpenOffice.org Impress documents.
1.Choose Insert - Picture - From File.
2.Select the file. In the File type box you can restrict the selection to certain file types.
3.Click the Link box if you want a link to the original file.
If the Link box is marked, whenever the document is updated and loaded the bitmap image is reloaded. The editing steps that you have carried out in the local copy of the image in the document are re-applied and the image is displayed.
If the Link box is not marked, you are always working with the copy created when the graphic was first inserted.
To embed graphics that were first inserted as links, go to Edit - Links and click the Break Link button.
4.Click Open to insert the image. |
ArielEnter wrote: | Quote: | | While making the program, I was afraid of leaving in the bit map table images that were no longer necessary every time Picture changed, making the document unnecessarily bigger and bigger. | The contents of an OpenDocument file can be extracted or viewed using a .zip archive utility. Assuming you have a zip archiving utility, the easiest way to see the contents of an OpenDocument file is to rename the OpenDocument file's extension to .zip and then double click it. The images are in the Pictures directory in the .zip file. |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Tue Nov 24, 2009 4:47 pm Post subject: Conclusion |
|
|
Once again, thanks James.
Well, my conclusion will be that OOo decides whether or not a picture is being used. If we delete all the records in the oBitmap variant the pictures are also deleted. But if a graphic object is also using one of the pictures, OOo doesn't delete it until this graphic object is deleted by the user. Well that solves everything.
I have tested already thanks to Jame's tip, and CangePicture (Mi macro) works perfectly without any waste of memory.
I'm so proud of the results, hope it helps a lot of people. |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Mon Aug 30, 2010 2:45 pm Post subject: problems with Change picture |
|
|
soundspaces said:
| Quote: |
hi, first of all thanks for yout work. It adds a feature to Open Office Writer very important and usefull to me.
I used it for a couple of days but today stopped to work suddently and apparently without reasons.
When I try to change the picture it returns and error that sounds like that:
"Select a picture to change. Unable to replace a picture"
It sounds very enigmatic to me.
Any help or suggestion will be appreciated!
Thanks again
SP
|
I'm very glad to have a feed back from my extension. Thank you very much.
About the problem you are having, well, the way I programed change picture, it can only identify one error, which happens when no image is being selected, if any other error happens then it tells you back the same message: "Select a picture to change. Unable to replace a picture".
have you experience this problem before? Could It have came with the new version of OOo? What version of OOo are you using? What OS are you using? Does it work in other machines? if it does, you may try to uninstall and install back OOo or the extension it self. Any clue you give me could help me to come up with an idea.
Mean while, I may further develop the code so that it gives back at least the error number and the system output. Maybe some one in the community will know what the problem is with that information.
Thanks soundspaces. Hope to hear from you. |
|
| Back to top |
|
 |
soundspaces General User

Joined: 27 Jul 2009 Posts: 14 Location: Italy
|
Posted: Mon Aug 30, 2010 10:24 pm Post subject: Re: problems with Change picture |
|
|
| ArielEnter wrote: |
I'm very glad to have a feed back from my extension. Thank you very much.
About the problem you are having, well, the way I programed change picture, it can only identify one error, which happens when no image is being selected, if any other error happens then it tells you back the same message: "Select a picture to change. Unable to replace a picture".
have you experience this problem before? Could It have came with the new version of OOo? What version of OOo are you using? What OS are you using? Does it work in other machines? if it does, you may try to uninstall and install back OOo or the extension it self. Any clue you give me could help me to come up with an idea.
Mean while, I may further develop the code so that it gives back at least the error number and the system output. Maybe some one in the community will know what the problem is with that information.
Thanks soundspaces. Hope to hear from you. |
Hi, thanks for answer. Sorry, I forget all basic info to let you handle that feedback.
1) As I said I installed changepicture plugin and for a couple of day everythings was OK, but suddently it stopped to work giving me that error.
2) I had that problem with OOo 3.2 but today I installed 3.2.1 and reinstalled ChangePicture plugin but nothing changes
3) I'm using Win 7 x64
4) Sorry I did not try in other machines
thanks for your work, unfortunatelly I'm not able to manage code, so I won't be more usefull for developper's comunity.
Please let us know when you release a new version of your plugin.
I think it's very usefull for a lot of us.
Cheers!
SP |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Mon Sep 06, 2010 10:29 am Post subject: |
|
|
Sorry to keep you waiting. For some reason I was not notify when I received a respond. I just upload the new release of Change Picture, 1.4.3.
soundspaces please down load it and install it in your openoffice. This version will be able to tell you more information that may help to understand what is going on with the extension.
Just try to use it and if an error happens it will pop up a dialog with information of the error. Post back what it tells you and maybe we will find out what is going on. |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Mon Sep 06, 2010 10:36 am Post subject: |
|
|
| P.S. If no message is given back, other than "Select a picture to change. Unable to replace a picture" then please tell me since it could give me some other clue of what the problem may be. |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Mon Sep 06, 2010 11:03 am Post subject: |
|
|
Hi soundspaces
If no error message is given back I think I know how to solve it right a way. If not please, tell me what the new message says. |
|
| Back to top |
|
 |
soundspaces General User

Joined: 27 Jul 2009 Posts: 14 Location: Italy
|
Posted: Tue Sep 14, 2010 1:22 am Post subject: |
|
|
Hi, sorry for late response. I just downloaded today the new latest version of picture changer and it is perfctly working. Actually I re-istalled my OS so I'm not in the same condition as when I asked you for help. Off course I'm still working on a win7 x64 machine and latest OOo version.
I'm going tell you if something changes or happens!
thanks for your work!
SP |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Tue Sep 14, 2010 11:50 am Post subject: |
|
|
| Quote: | | ...I re-istalled my OS... |
Oh, sorry to hear that, I hope you didn't have to do it only to make this extension work. Hope that the problem won't come back again for you, but I wish we could have gone through it a little further which would have helped to prevent future problems for other people as well. Oh well, better gone than the pain of keep having the trouble around.
By the way, I'll be very glad to hear how and what are you using the extension for, do you use it constantly? Also, I'll like to know where are you come from and things like that. Is just that is the first time I found some one that appreciated my job and gives me a feed back. I'll like to chat some day using a Instant Message client in IRC or gmail. Anyway thanks for the words of appreciation see you later. |
|
| Back to top |
|
 |
Tonttu General User

Joined: 24 Jun 2010 Posts: 8
|
Posted: Sat Apr 02, 2011 11:36 am Post subject: |
|
|
Trying to install the extension on LO 3.3.2 I get a runtime exception for "bad root element <node>"
Any plans for an update? |
|
| Back to top |
|
 |
ArielEnter General User

Joined: 11 Jun 2009 Posts: 32
|
Posted: Sat Apr 02, 2011 7:48 pm Post subject: |
|
|
| I'll try to fix it next week. Thank you for the reply. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|