Visio Guy

Visio Discussions => General Visio => Topic started by: thalantyrdsl on July 12, 2017, 10:13:45 PM

Title: Image Placeholder
Post by: thalantyrdsl on July 12, 2017, 10:13:45 PM
Hi there,

First time poster.

I currently work through building plans for a fibre company. As a part of the plans I provide them, we send photos of the route we're going to use.
At the moment I try to keep all photos that I take in portrait mode, drag and drop them into Visio, align them, etc.
The downside is that each page has x8 photos of it and I'd prefer not to have to re-align photos every time I added them to Visio.

I was wondering if there was a 'placeholder' shape/stencil that could be inserted, where you then click it and browse to the image you want to add, which then auto re-sizes to that shape size?

Please let me know if I need to explain it better.

Thank you.
Title: Re: Image Placeholder
Post by: wapperdude on July 12, 2017, 10:46:27 PM
Would this work?  https://support.office.com/en-us/article/Align-and-position-shapes-in-a-diagram-9de5bafb-676e-44e0-b59a-ed82951e4da9 (https://support.office.com/en-us/article/Align-and-position-shapes-in-a-diagram-9de5bafb-676e-44e0-b59a-ed82951e4da9)

Wapperdude
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 12, 2017, 11:41:20 PM
Most of that involves selecting and aligning things. I wanted to try and avoid that work every time I insert pictures.

Please see below some better pictures in what I'm trying to do.

Picture #1 is how the end result works out.
Picture #2 is somewhere along the lines of what I'd like, which are shapes locked in place, but allow me to insert a picture somehow, and auto-size it to the size of that shape.

Thank you.
Title: Re: Image Placeholder
Post by: wapperdude on July 13, 2017, 12:11:26 AM
That's doable, well, pretty sure.  Takes some code development.

Which version of Visio?

My initial thought is something like this...
1)  place 8 shapes / containers on the page as placeholders like you've shown.
2)  drop your picture on the page...the mouse should be within one of the placeholders...that will be picture center
3)  use spatial relations to determine which shape geometry contains the picture center.
         (Other techniques are possible)
4)  set pic Pin X & Pin Y to be center of contAiner
5) resize picture to fit within container boundaries.

That would be it.  The macro automatically fires when picture is dropped on the page.

Wapperdude
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 13, 2017, 12:38:05 AM
Thank you for the reply. I understand about half of what you've put above, which is promising that it may be possible.
In terms of the next step forward, is this something that I'd need to engage a coder or Visio expert to do? Freelancer.com?
Title: Re: Image Placeholder
Post by: wapperdude on July 13, 2017, 06:11:03 AM
Do you do any coding at all?

Your next step depends upon your urgency.  Among the volunteer contributors at this forum, there are several that are capable of such coding.  But, their time is limited and sporadic.  So, it could take a couple days, or more.  Not sure how you might secure someone one on a commission basis.

Wapperdude
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 13, 2017, 06:24:33 AM
I don't do this sort of coding. I'd be limited to HTML/CSS and it's been a while since I did any coding work.
I'm not in a huge hurry. This is more of an efficiency thing to speed up the plans I do and also ensure they're nice and clean.

How would I go about requesting someone from the forums take a look over this? Thank you.
Title: Re: Image Placeholder
Post by: metuemre on July 13, 2017, 10:28:37 AM
Here is a simple template as starting point. You can improve it if you like
Title: Re: Image Placeholder
Post by: wapperdude on July 13, 2017, 03:22:31 PM
Most impressive!  Simple indeed! :D  Yes simple to use, but the development and code was hardly simple.  You pulled this together in amazingly short order.

Nice job metuemre.  Works very well.

wapperdude
Title: Re: Image Placeholder
Post by: Yacine on July 13, 2017, 04:50:32 PM
I also got inspired by the question and used Wapperdude's idea of a container.
Otherwise the same image handling as metuemre.

At the same time I solved the issue of the "relationships" issue, which fires only ones. The trick is to catch the modifaction by a dependson call, do all the necessary handling, then delete the value in the relationsships field. It's now empty again and reacts to new drops. http://visguy.com/vgforum/index.php?topic=4554.0


Title: Re: Image Placeholder
Post by: wapperdude on July 13, 2017, 07:04:34 PM
Well, how could I resist?!?

Naturally, I had to take a different approach.  The idea is any page may be used.  Assumes page is blank, i.e., no shapes.  It also assumes the target (final) shape width and height.  These are hard coded, but could be adjusted.  Finally, it defines the picture locations.

Any shape or picture may be dropped.  These are automatically placed left to right, top to bottom.

Doesn't use a mask shape or containers...(for those of us in the dark ages.)

Enjoy!
Wapperdude
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 02:40:11 AM
Wow guys. Thank you very much. I just got back to this post and am going over what you put together. Thank you.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 02:53:56 AM
Quote from: metuemre on July 13, 2017, 10:28:37 AMSolution

Thank you Metuemre. When I try to run the script that you wrote I get the attached error regarding a 64-bit operating system. I'm unsure how to approach this.

Quote from: Yacine on July 13, 2017, 04:50:32 PMSolution

Thank you Yacine. This is very easy to use. I think I need to make a few adjustments to the sizes so that it fits perfectly for photograph dimensions.
When I currently drop in a 480x640 image it sizes it smaller than the actual bounding box, both vertically and horizontally.
How would I get to the code to look at if this is easy to re-size the dimensions so the dropped image fits perfectly?

Quote from: wapperdude on July 13, 2017, 07:04:34 PMSolution

Thank you Wapperdude. I'm having troubles getting the image to drop into any of the actual shapes.
I drag and drop the image from outside Visio in and then try to move it over the top of the shapes but it won't snap in.
Am I looking at your solution from the wrong point of view? Thank you.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 03:39:10 AM
For my solution, the template shows example of final solution.  As mentioned, start with a blank drawing page, either delete the shapes on the existing page, or merely add a page.  Then, just find a picture you want drag, drop, release.  It places and resizes itself.  It's a rather simple approach, and lacks the flexibility of the others.  Specifically, it merely counts shapes and places them in a pre-arranged order.  So, if you delete a shape, it won't drop to the empty spot.  My goal was pure simplicity.

As for Metuemre's solution, I'm surprised there's an issue.  The code is involved, so I didn't go thru it.  It did run just fine on my PC, which is a 64 bit machine.  I'm assuming you're running 32 bit.  First thing to try is re-saving the file on your machine.  Then try to run that.  Might clear up the 32 / 64 issue.

As for Yacine's approach, I cannot look at it, as it is for newer Visio versions.

For any of these approach's, once the Visio file is open, if you press <alt> + F11, that will bring up the VBA window.  Typically, in left column, you'll see Visio Objects > ThisDocument.  Dbl click ThisDocument and you'll see the code in the right window.

Wapperdude.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 04:09:29 AM
Quote from: wapperdude on July 14, 2017, 03:39:10 AM
For my solution, the template shows example of final solution.  As mentioned, start with a blank drawing page, either delete the shapes on the existing page, or merely add a page.  Then, just find a picture you want drag, drop, release.  It places and resizes itself.  It's a rather simple approach, and lacks the flexibility of the others.  Specifically, it merely counts shapes and places them in a pre-arranged order.  So, if you delete a shape, it won't drop to the empty spot.  My goal was pure simplicity.

Ahhh. I see what you mean. That's quite handy. I also see what you mean by it not dropping into an empty spot. I deleted a few and it'll place it on top of another, etc.

Quote from: wapperdude on July 14, 2017, 03:39:10 AMAs for Metuemre's solution, I'm surprised there's an issue.  The code is involved, so I didn't go thru it.  It did run just fine on my PC, which is a 64 bit machine.  I'm assuming you're running 32 bit.  First thing to try is re-saving the file on your machine.  Then try to run that.  Might clear up the 32 / 64 issue.

I'm definitely running 64-bit Windows 10, with Visio 2013. I've tried opening both the .VSD and .VSDM files, which open fine, but when double-clicking it gives the previous error. I may be doing something wrong here? Macros seem enabled. I've tried to re-save it on my machine, re-enable macros and it gives the same error unfortunately.

Quote from: wapperdude on July 14, 2017, 03:39:10 AMAs for Yacine's approach, I cannot look at it, as it is for newer Visio versions.

This one seems to work the cleanest so far, but has sizing issues, which I believe can be very easily fixed.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 04:18:33 AM
Updated.  Simple change.  Will now fill in missing spots.

Cheers.
wapperdude

PS.  Other options, delete one or more shapes, then do a copy paste of existing shape.  It will fill in open spot.  Other:  use insert picture from file, or just draw a shape.  Hmmm.  Jus t occurred to me, macro always fires.  That will be a problem.  May have to hard code the page, then inhibit if not correct page.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 04:44:59 AM
OK.  One more modification.  Code modified to place pictures on page named "Pics".  If any other page is active, then macro will not do anything.

Wapperdude
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 04:54:55 AM
Quote from: wapperdude on July 14, 2017, 04:44:59 AMOK.  One more modification.  Code modified to place pictures on page named "Pics".  If any other page is active, then macro will not do anything.

There looks to be a bug when deleting an image it's not resetting the 'max' number of images allowed.
Eg: I delete 8, then add 1 and it says max number reached, though it places that last image in the centre of the page anyway.

The placement seems to be working great.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 05:09:09 AM
The macro was all talk, no action!   ;D

Now it backs up it words!   ;)
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 05:53:15 AM
Quote from: wapperdude on July 14, 2017, 05:09:09 AM
The macro was all talk, no action!   ;D
Now it backs up it words!   ;)

Awesome. That's stopping the picture from being pasted as per the popup message, but the counter doesn't seem to be reducing as images are removed.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 05:56:38 AM
Also, I assume that Visio treats 'images' as shapes? Is there a way to differentiate between shapes and images?
The reason I ask is that I will need to place other information over the top of the images, such as a 'number reference' and also a 'text box' with a description.

If I paste on x8 images and then try to add a text box it looks to be counting them as shapes.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 05:58:10 AM
Quote from: metuemre on July 13, 2017, 10:28:37 AMSolution
Thank you Metuemre. When I try to run the script that you wrote I get the attached error regarding a 64-bit operating system. I'm unsure how to approach this.[/quote]

Hi Metuemre. I was wondering if you knew what the problem could be here? I'd love to get the double-click insert image working if possible. Thank you.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 06:13:32 AM
The "counter" isn't tracking number of shapes on the page.  Rather, it looks at shape ID's.  This was a quick and dirty solution and relies on known Visio behavior. 

So, 1st shape placed, Visio considers as sheet.1, 2nd is sheet.2 up thru sheet.8.  Let's say, 6 shapes have been placed, and you decide 4th shape should be deleted.  The next shape placed will be a new sheet.4, not sheet.7.  New sheet.4 replaces previously deleted sheet.4.  The next shape will be sheet.7.  So, had sheets 2, 4, 5 been deleted, they would be replaced sequentially before more are added.  Thus, the code leverages the behavior of Visio to remain relatively simple in structure.  Perhaps not very elegant or robust, but it gets the job done. 

Update:
Ah.  The task definition has changed.  Unlike Metuemre's (and Yacine's) solution(s), this approach just grabs whatever comes its way.  It's a strength and weakness that the other approaches avoid.  Visio treats all shapes as sheets.xyz. Not sure if there's an easy way to distinguish a picture from other shape types.

Wapperdude.
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 06:15:56 AM
Hi thalantyrdsl, is your Visio 32bit or 64bit? My template works on 32bit Visio only
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 06:21:00 AM
Quote from: metuemre on July 14, 2017, 06:15:56 AM
Hi thalantyrdsl, is your Visio 32bit or 64bit? My template works on 32bit Visio only

I run Windows 64-bit so I assume Visio is 64-bit.
Is there a way to make the template work on both 32-bit & 64-bit, so it's not limited based on versions?

Thank you.
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 06:25:29 AM
Quote from: wapperdude on July 13, 2017, 03:22:31 PM
Most impressive!  Simple indeed! :D  Yes simple to use, but the development and code was hardly simple.  You pulled this together in amazingly short order.

Nice job metuemre.  Works very well.

wapperdude

Thanks wapperdude but most of the credit goes to AndyW for CComDlg class  :D

http://visguy.com/vgforum/index.php?topic=738.0 (http://visguy.com/vgforum/index.php?topic=738.0)
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 06:25:52 AM
Quote from: wapperdude on July 14, 2017, 06:13:32 AM
The "counter" isn't tracking number of shapes on the page.  Rather, it looks at shape ID's.  This was a quick and dirty solution and relies on known Visio behavior. 

So, 1st shape placed, Visio considers as sheet.1, 2nd is sheet.2 up thru sheet.8.  Let's say, 6 shapes have been placed, and you decide 4th shape should be deleted.  The next shape placed will be a new sheet.4, not sheet.7.  New sheet.4 replaces previously deleted sheet.4.  The next shape will be sheet.7.  So, had sheets 2, 4, 5 been deleted, they would be replaced sequentially before more are added.  Thus, the code leverages the behavior of Visio to remain relatively simple in structure.  Perhaps not very elegant or robust, but it gets the job done. 

Wapperdude.

Thanks Wapper. Sorry if I'm getting confused.
These are the steps I've taken to get the error:

- Open your attachment
- Delete shapes 1,2,3,4,6,7,8, leaving #5
- Copy & Paste #5
- It pastes into location 6
- Paste it another two times, pastes into location 7 & 8
- Paste it a fourth time, comes up with the error about too many shapes
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 06:31:23 AM
Quote from: thalantyrdsl on July 14, 2017, 06:21:00 AM
Quote from: metuemre on July 14, 2017, 06:15:56 AM
Hi thalantyrdsl, is your Visio 32bit or 64bit? My template works on 32bit Visio only

I run Windows 64-bit so I assume Visio is 64-bit.
Is there a way to make the template work on both 32-bit & 64-bit, so it's not limited based on versions?

Thank you.

It is possible to install 32bit or 64bit Visio on 64bit operating system. Assuming that Visio is 64bit, please try the version attached
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 06:36:25 AM
Quote from: metuemre on July 14, 2017, 06:31:23 AM
It is possible to install 32bit or 64bit Visio on 64bit operating system. Assuming that Visio is 64bit, please try the version attached

That didn't seem to fix the issue for me. Same error as previously attached.

It may be something that I'm doing. Can I please ask why there are two attachments?
Do I need to add something in before the script will work?
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 06:49:23 AM
Please try this one.

.vsd file is for those who have Visio 2010 or older versions
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 06:52:35 AM
Quote from: metuemre on July 14, 2017, 06:49:23 AM
Please try this one.
.vsd file is for those who have Visio 2010 or older versions

That works fine now, and wow, that's awesome!

The only issue I can see is that it's rotating images 90-degrees counter-clockwise, and then stretching them.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 06:56:18 AM
QuoteThanks Wapper. Sorry if I'm getting confused.
These are the steps I've taken to get the error:

That's not how it should work.  It is supposed to start adding shapes at ID = 1, not 6.  Seems as though Visio is behaving differently on your PC.  That may be a behavior change since V2007.

I'll have to see if there's an easy, universal fix.  As for distinguishing pictures, I see no such object type in the Visio model.  The best would be to allow more than 8 shapes.  The 1st 8 will be placed.  If you have fewer than 8 pictures, you could put blank shapes in those spots.  Then add the other items as desired.  But, that complicates things if you subsequently want to add more pictures.

So, I think the other two approaches are better options.

Wapperdude
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 07:01:52 AM
Quote from: thalantyrdsl on July 14, 2017, 06:52:35 AM
Quote from: metuemre on July 14, 2017, 06:49:23 AM
Please try this one.
.vsd file is for those who have Visio 2010 or older versions

That works fine now, and wow, that's awesome!

The only issue I can see is that it's rotating images 90-degrees counter-clockwise, and then stretching them.

I used the same command in the code with Insert picture button on the Ribbon menu. You can try to insert the same picture with Insert-Pictures button on the ribbon and see if the picture is rotated. In my computer pictures are inserted as it is, not rotated
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 07:13:05 AM
Quote from: metuemre on July 14, 2017, 07:01:52 AM
I used the same command in the code with Insert picture button on the Ribbon menu. You can try to insert the same picture with Insert-Pictures button on the ribbon and see if the picture is rotated. In my computer pictures are inserted as it is, not rotated

Ok, I tested it by 'Insert Picture'. It did the same thing.
I then opened my template file that I use and tried it again, and it inserted it correctly, without rotating.

There's something different between the two documents. The one I'm using is .vsdx, but I don't know if it was created originally as a .vsd or .vsdx

Any thoughts on why one would insert correctly and one rotate?
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 07:14:25 AM
Quote from: wapperdude on July 14, 2017, 06:56:18 AM
QuoteThanks Wapper. Sorry if I'm getting confused.
These are the steps I've taken to get the error:

That's not how it should work.  It is supposed to start adding shapes at ID = 1, not 6.  Seems as though Visio is behaving differently on your PC.  That may be a behavior change since V2007.

I'll have to see if there's an easy, universal fix.  As for distinguishing pictures, I see no such object type in the Visio model.  The best would be to allow more than 8 shapes.  The 1st 8 will be placed.  If you have fewer than 8 pictures, you could put blank shapes in those spots.  Then add the other items as desired.  But, that complicates things if you subsequently want to add more pictures.

So, I think the other two approaches are better options.

Wapperdude

Thank you very much for the help Wapper. That code will come in handy for something else that I do along the way. The ideal script is definitely for a double click insert as then I can edit the page, lock things down, and preset everything where I want it. Thank you again.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 07:48:11 AM
Quote from: metuemre on July 14, 2017, 07:01:52 AMI used the same command in the code with Insert picture button on the Ribbon menu. You can try to insert the same picture with Insert-Pictures button on the ribbon and see if the picture is rotated. In my computer pictures are inserted as it is, not rotated

Is there a way for me to add your code to my current template? That may work out better as I'll end up having to do it anyway.
Title: Re: Image Placeholder
Post by: metuemre on July 14, 2017, 08:05:03 AM
You can import attached code files to your template.

Open the VBA Editor with Developer-Visual Basic button. Then right click on wherever you want at Project Explorer on the left and click Import File and select attached files. After that copy the 8 frame shapes and paste to your template and you are ready to go.

Title: Re: Image Placeholder
Post by: Yacine on July 14, 2017, 08:16:27 AM
@Wapperdude, your solution is also very nice.
Some thoughts however:
- instead of working with shape IDs with hard coded positions, you might divide the canvas into rows and columns by defining custom props "rows" and "columns". Depending upon where the picture was placed (its pinX and pinY), you could recalculate the position, depending upon the region (row/column). rowNum = intup(pinX/pagewidth*thepage!prop.rows) and vice versa for the column.
Title: Re: Image Placeholder
Post by: thalantyrdsl on July 14, 2017, 08:52:53 AM
Quote from: metuemre on July 14, 2017, 08:05:03 AM
You can import attached code files to your template.

Open the VBA Editor with Developer-Visual Basic button. Then right click on wherever you want at Project Explorer on the left and click Import File and select attached files. After that copy the 8 frame shapes and paste to your template and you are ready to go.

That is awesome Metuemre! I've imported it and it works perfectly. I've used it the same image for my cover page also. Thank you very much, it's exactly what I wanted.

Thank you to everyone who chipped in with this one. I really appreciate it.
Title: Re: Image Placeholder
Post by: wapperdude on July 14, 2017, 10:26:58 PM
Well, even though the horse is out of the barn, I apparently have too much idle time.  So, couldn't leave this with incomplete solution.  Bit the bullet and abandoned the auto placement approach.  Instead, took an approach similar to Metuemre's, i.e., double click a shape to set the location of the picture.  All of the sizing / aspect ratio stuff is holdover from before.  Added the Windows Explorer method to find the file.  Also added a right click option to the page to show / hide the shape placement locators.  Gives the option to hide the unused locators.

Enjoy.
Now I can rest in peace...unless a bug is discovered.   :o

wapperdude
Title: Re: Image Placeholder
Post by: Crazy Squirrel on January 20, 2022, 05:29:09 PM
Quote from: metuemre on July 14, 2017, 08:05:03 AM
You can import attached code files to your template.

Open the VBA Editor with Developer-Visual Basic button. Then right click on wherever you want at Project Explorer on the left and click Import File and select attached files. After that copy the 8 frame shapes and paste to your template and you are ready to go.

Hi metuemre,

Is there a way to add a picture but have it locked to the shape?  i.e. if the picture imported is rotated 90 degrees, it stays within the shape?
Title: Re: Image Placeholder
Post by: wapperdude on January 21, 2022, 11:31:17 PM
In metujemre's solution, ImportPicture.vsdm, edit the ImportPic module and replace the If .ShowOpen code section with code below.  This will invoke a MSGBOX after initial picture placement and asks rotation is desired.  I did it this way so that picture is viewable after placement.  IF answer is No, then all proceeds as before.  If Yes, picture is rotated and properly sized to fit the placement frame and maintain the picture aspect ratio.


        If .ShowOpen Then
            strfilename = .FileName
            Application.ActiveWindow.Page.Import strfilename
            Set Pic = ActiveWindow.Selection.PrimaryItem
            aspRat = Pic.Cells("Height").Result("") / Pic.Cells("Width").Result("")
            usrAns = MsgBox("Rotate Pic?", vbYesNo, "User Choice")
            Pic.Cells("PinX").Formula = Frame.Cells("PinX").Result("")
            Pic.Cells("PinY").Formula = Frame.Cells("PinY").Result("")
            If usrAns = vbYes Then
                Pic.Cells("Angle").Formula = "90 deg"
                Pic.Cells("Width").Formula = Frame.Cells("Height").Result("")
                Pic.Cells("Height").Formula = Pic.Cells("Width").Result("") * aspRat
            Else
                Pic.Cells("Width").Formula = Frame.Cells("Width").Result("")
                Pic.Cells("Height").Formula = Frame.Cells("Height").Result("")
            End If
        End If