C# SolidWorks secondary development API ---Get preview

Recently, some netizens asked how to simply display a preview image on the interface, which is similar to the image displayed in the Explorer.
Today, I will take everyone to find it. First, search in the Chinese translation I shared: " Preview "
Insert picture description here
we found several useful ones.
Look at this one first: The first column is the address of the Web help, of course, you can also check it in the local api help.
Insert picture description here
Insert picture description here
There is another example in vba. The Insert picture description here
problem is that this help can only be used in-process, like exe is not enough.
Continue to look at the following, there is another method:
Insert picture description here
This method seems to be used according to the description:
Gets the specified preview bitmap of a document and saves it as a Windows bitmap file (.bmp) using the specified filename.
Insert picture description here
Write the code:

ISldWorks swApp = Utility.ConnectToSolidWorks();

            //此处路径请自己确保存在。
            string fileName = @"D:\09_Study\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\bodies.sldasm";

            string configName = "Default";

            string bitmapPathName = @"D:\09_Study\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\bodies.bmp";

            var status = swApp.GetPreviewBitmapFile(fileName, configName, bitmapPathName);

            if (System.IO.File.Exists(bitmapPathName))
            {
    
    
                swApp.SendMsgToUser("预览图获取完成。");
            }

The resulting color is a bit strange and not ideal.
Insert picture description here
After thinking about it here, we can directly get the preview of the system resource manager without using solidworks to process it.
This is directly Baidu, just look for resources.
I found a class and called it directly to get the file. The suffix is ​​.li, which is a very strange format.
Insert picture description here
Find the file and copy it to the current part folder. Change the suffix to .bmp, ok.
Insert picture description here

This is obtained by using the system's api. It would be perfect if it is only a single configuration part. This seems to be unable to obtain previews of different configurations.

When querying the api help, it was found that the function of obtaining preview images is also provided in the Document Manager API.
Note that a key is required here. For genuine users, please find the corresponding agent to obtain it. The key for each version is different and the company name is included.

Insert picture description here
There are still examples, so let's try it out: during
testing, we found that document mgr only supports 64-bit programs starting from 2018. Therefore, when testing, you have to change the compiler option to x64 bit, otherwise it will always report that the dll instance cannot be created.

 			const string sLicenseKey = "your_license_key"; //这个不好公开。请下载代码            

            string sDocFileName = fileName;

            SwDMClassFactory swClassFact = default(SwDMClassFactory);
            SwDMApplication swDocMgr = default(SwDMApplication);
            SwDMDocument swDoc = default(SwDMDocument);
            SwDMDocument10 swDoc10 = default(SwDMDocument10);
            SwDmDocumentType nDocType = 0;
            SwDmDocumentOpenError nRetVal = 0;
            SwDmPreviewError nError = 0;

            // Determine type of SOLIDWORKS file based on file extension
            if (sDocFileName.EndsWith("sldprt"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentPart;
            }
            else if (sDocFileName.EndsWith("sldasm"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentAssembly;
            }
            else if (sDocFileName.EndsWith("slddrw"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentDrawing;
            }
            else
            {
    
    
                // Probably not a SOLIDWORKS file,
                // so cannot open
                nDocType = SwDmDocumentType.swDmDocumentUnknown;
                return;
            }

            swClassFact = new SwDMClassFactory();
            swDocMgr = (SwDMApplication)swClassFact.GetApplication(sLicenseKey);
            swDoc = (SwDMDocument)swDocMgr.GetDocument(sDocFileName, nDocType, true, out nRetVal);
            Debug.Print("File = " + swDoc.FullName);
            Debug.Print("  Version          = " + swDoc.GetVersion());
            Debug.Print("  Author           = " + swDoc.Author);
            Debug.Print("  Comments         = " + swDoc.Comments);
            Debug.Print("  CreationDate     = " + swDoc.CreationDate);
            Debug.Print("  Keywords         = " + swDoc.Keywords);
            Debug.Print("  LastSavedBy      = " + swDoc.LastSavedBy);
            Debug.Print("  LastSavedDate    = " + swDoc.LastSavedDate);
            Debug.Print("  Subject          = " + swDoc.Subject);
            Debug.Print("  Title            = " + swDoc.Title);

            swDoc10 = (SwDMDocument10)swDoc;
            // SwDMDocument10::GetPreviewBitmap throws an unmanaged COM exception
            // for out-of-process C# console applications
            // Use the following code in SOLIDWORKS C# macros and add-ins
            object objBitMap = swDoc10.GetPreviewBitmap(out nError);
            System.Drawing.Image imgPreview = PictureDispConverter.Convert(objBitMap);
            imgPreview.Save(bitmapPathName, System.Drawing.Imaging.ImageFormat.Bmp);
            imgPreview.Dispose();

            Debug.Print("    Preview stream   = " + swDoc10.PreviewStreamName);

After the execution is successful, the preview image with the wrong color is replaced successfully.

Insert picture description here

Of course, it is still only the thumbnail of the last saved configuration.
How to obtain other configurations?
There are instructions in the remarks of this method: Other methods are needed:
Insert picture description here

There is an example:
Get PNG Preview Bitmap and Stream for Configuration Example (C#) The
core is here, you need to traverse the configuration. Of course, if you know the configuration name, you don't need to traverse it once.
I have not tested the following code

 swCfgMgr = swDoc.ConfigurationManager;
 
                Debug.Print("File = " + swDoc.FullName);
                Debug.Print("Active configuration name = " + swCfgMgr.GetActiveConfigurationName());
                vCfgNameArr = (string[])swCfgMgr.GetConfigurationNames();
 
                foreach (string vCfgName in vCfgNameArr)
                {
    
    
 
                    swCfg = (SwDMConfiguration7)swCfgMgr.GetConfigurationByName(vCfgName);
                    // SwDMConfiguration7::GetPreviewPNGBitmap throws an unmanaged COM exception 
                    // for out-of-process C# console applications
                    // Use the following code in SOLIDWORKS C# macros and add-ins  
                    object objBitMap = swCfg.GetPreviewPNGBitmap(out nError);
                    System.Drawing.Image imgPreview = PictureDispConverter.Convert(objBitMap);
                    imgPreview.Save("C:\\temp\\" + vCfgName + ".PNG", System.Drawing.Imaging.ImageFormat.Png);
                    imgPreview.Dispose();
 
                    Debug.Print("   " + vCfgName);
                    Debug.Print("     PNG preview stream = " + swCfg.PreviewPNGStreamName);
 
                    Debug.Print(" ");
                }

Guess you like

Origin blog.csdn.net/zengqh0314/article/details/106011590