Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

VB.net scaling / Resize and export as SVG

Community Beginner ,
Feb 22, 2011 Feb 22, 2011

Hi,

I'm using Illustrator CS4 and VB.NET 2008.

I'm doing a fairly simple operation in VB.net using Illustrator.  I load an  .AI file that was saved out of Illustrator and then resave it as a SVG file.  This works fine.

I need to scale up the SVG since for whatever reason the SVG artwork is smaller than the AI file.    This happens even if you just save the SVG file straight out of CS2/CS3/CS4/CS5 not using any automation.  Must be a feature, not a bug.

Anyway, I am able to set the scalematrix but for whatever reason, when I try to apply it, I can't get the syntax or object reference right.

I've looked at the illustrator CS4 Scripting reference(see below) and it says " artItem.Transform totalMatrix".  If I use that syntax  VB.net complains that the "Method arguments must be enclosed in parethesis"

If I enclose it,Like this: artitem.Transform(scalematrix)

I get this Warning     "Variable 'artitem' is used before it has been assigned a value. A null reference exception could result at runtime. "  and of course, at run time it barfs with a null exception.

Any suggestions?  I've dug through the illustrator objects trying to see if I've defined artitem wrong or something.  I'm stuck.

Thanks,

Mark


---------

      Dim illusapp As New Illustrator.Application
      Dim illusdoc As Illustrator.Document
      Dim scalematrix As Illustrator.Matrix
      Dim artitem As Illustrator.PathItem
      illusapp.UserInteractionLevel = Illustrator.AiUserInteractionLevel.aiDontDisplayAlerts
      
      Dim svgOpt = new illustrator.ExportOptionsSVG
      svgOpt.DTD = Illustrator.AiSVGDTDVersion.aiSVG1_0   

            illusdoc = illusapp.Open("c:\test\test.ai")

            scalematrix = illusapp.GetScaleMatrix(125,125)

'------ Problem starts here.

            artitem.transform(totalmatrix)
'------ Problem ends here.

            illusdoc.Export("c:\test\test.svg",Illustrator.AiExportType.aiSVG,svgopt)
            illusdoc.Close

-------

From Adobe Illustrator CS4 Scripting Reference.

     Applying transformations with a matrix
'Creates a new translation and rotation matrix then
'applies it to all items in the current document


Set appRef = CreateObject("Illustrator.Application")

'Move art half an inch to the right and 1.5 inch up on the page

     Set moveMatrix = appRef.GetTranslationMatrix(72 * 0.5, 72 * 1.5)

'Add a rotation to the translation -- 10 degrees counterclockwise

     Set totalMatrix = appRef.ConcatenateRotationMatrix(moveMatrix, 10)

'Apply the transformation to all art in the document

For Each artItem In appRef.ActiveDocument.PageItems
     artItem.Transform totalMatrix
Next

TOPICS
Scripting
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 22, 2011 Feb 22, 2011

oh, I see, you need to apply the matrix to an object, in this case artitem doesn't exist yet

you're missing this part

'Apply the transformation to all art in the document

For Each artItem In appRef.ActiveDocument.PageItems
     artItem.Transform totalMatrix
Next

Translate
Adobe
Community Expert ,
Feb 22, 2011 Feb 22, 2011

in the sample totalMatrix = translation + rotation, since you only need scaling you apply the matrix to scaling only

artitem.transform(scalematrix)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2011 Feb 22, 2011

Hi,

Thanks for responding.

In my original post, I had tried that.  Totalmatrix was from the CS4 scripting reference so I had the wrong one in there, I apologize for any confusion.

I've also tried assigning it to a new object but also get an error.  I realize I'm not instancing something it wants, I'm just at a loss as to figure out how.

illustratorscaling.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2011 Feb 22, 2011

oh, I see, you need to apply the matrix to an object, in this case artitem doesn't exist yet

you're missing this part

'Apply the transformation to all art in the document

For Each artItem In appRef.ActiveDocument.PageItems
     artItem.Transform totalMatrix
Next

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2011 Feb 22, 2011

        Ok, I figured out the problem.  I had read that snippet before and tried it and it didn't work.  I then Dim'd artitem as a compoundpathitem instead of pathitem and it worked on the first iteration of the for each loop.   One the second trip through there was an invalid cast exception..but it had upsized the art!   So I just added try/catch to handle the error and it works!

For future reference, anyone that reads this will need to add a reference to the CS4(or whichever version) type library COM object, change the hard coded file paths and it should convert from AI to SVG and Scale it up or down by replacing the 125,125 in the scale matrix by whatever percentage you'd like.   The Userinteractionlevel disables any warnings so that it runs automatic.

Thanks again Carlos...while you didn't give me the exact code I needed, you pointed me at what I had previously dismissed due to a different error.

-Mark

-------------

        Dim illusapp As New Illustrator.Application
        Dim illusdoc As Illustrator.Document
        Dim scalematrix As Illustrator.Matrix
        Dim artitem As Illustrator.CompoundPathItem
       
        Dim svgOpt = new illustrator.ExportOptionsSVG
        svgOpt.DTD = Illustrator.AiSVGDTDVersion.aiSVG1_0    
        illusapp.UserInteractionLevel = Illustrator.AiUserInteractionLevel.aiDontDisplayAlerts

            illusdoc = illusapp.Open("c:\test\test.ai")
            scalematrix = illusapp.GetScaleMatrix(125,125)

            Try
                For Each artItem In illusapp.ActiveDocument.PageItems
                    artitem.Transform(scalematrix)
                Next
            Catch
            End Try

            illusdoc.Export("C:\test\test.svg",Illustrator.AiExportType.aiSVG,svgopt)
            illusdoc.Close

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2011 Feb 22, 2011
LATEST

you're welcome


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines